Skip to main content
Module

x/unknownutil/is.ts>isOptionalOf

🦕 A lightweight utility pack for handling unknown type
Go to Latest
function isOptionalOf
import { isOptionalOf } from "https://deno.land/x/unknownutil@v3.18.0/is.ts";

Return an Optional annotated type predicate function that returns true if the type of x is T or undefined.

To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.

import { is } from "@core/unknownutil";

const isMyType = is.OptionalOf(is.String);
const a: unknown = "a";
if (isMyType(a)) {
  // a is narrowed to string | undefined
  const _: string | undefined = a;
}

Returns

Predicate<T | undefined> & WithMetadata<IsOptionalOfMetadata>