Skip to main content
Module

x/unknownutil/is.ts>isLiteralOneOf

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

Return a type predicate function that returns true if the type of x is one of literal type in preds.

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

import { is } from "https://deno.land/x/unknownutil@v3.16.1/mod.ts";

const isMyType = is.LiteralOneOf(["hello", "world"] as const);
const a: unknown = "hello";
if (isMyType(a)) {
  // a is narrowed to "hello" | "world"
  const _: "hello" | "world" = a;
}

Type Parameters

T extends readonly Primitive[]

Parameters

literals: T

Returns

Predicate<T[number]> & WithMetadata<IsLiteralOneOfMetadata>