import { AssertionError } from "../../assertion-error.js"; import { desc } from "../../describe/describe.js"; import { oneOf } from "./one-of.match.js"; type OneOfAssertion = [ Extract, ] extends [never] ? TActual & TAllowed[number] : Extract; export function assertOneOf( value: TActual, allowed: TAllowed, message?: string, ): asserts value is OneOfAssertion; export function assertOneOf( value: unknown, allowed: TAllowed, message?: string, ): asserts value is TAllowed[number]; /** * Assert that a value is one of a set of expected values, with type-narrowing. */ export function assertOneOf( value: unknown, allowed: TAllowed, message?: string, ): void { const matcher = oneOf(allowed); if (!matcher.matches(value)) { throw new AssertionError( message ?? `Expected ${desc(value)} to be ${matcher.describe()}.`, value, matcher.represent(), ); } }