isAmong

Let's say we have an union type like:

Names.ts
export const names = ["foo", "bar", "baz"] as const;
export type Name = typeof names[number];

isAmong enables to test if a given values is one of the names

import { isAmong } from "tsafe/isAmong";
import { names, type Names } from "./Names";

declare value: "foo" | "bar" | "something else";

if( isAmong(names, value) ){
  // Here value is of type "foo" | "bar"
  // (the intesection of the type of value before the test and Name)
}

If we just have the type and not the exhausive array:

import { isAmong } from "tsafe/isAmong";
import { assert, type Equals } from "tsafe/assert";
import type { Names } from "./Names";

const names = ["foo", "bar", "baz"] as const;

assert<Equals<typeof names, Names>>;

declare value: string;

if( isAmong(names, value) ){
   // Here value is of type Names
}

Last updated