Skip to content

match

match<C, A, Exhaustive>(exhaustive: boolean, casesObj: C, key: string, …params: any[]): A

Defined in: match-case.ts:22

Pattern-matching utility for handling cases by key. Invokes the handler for the given key, or the fallback ’_’ handler if present. Throws an error if no handler is found and exhaustive is true.

Type ParameterDefault typeDescription
C extends Cases<any, A> & { _?: undefined; } | PartialCases<any, A> | Partial<Cases<any, A> & { _: (variant: any) => A; }>-Cases object type
A-Return type of handlers
Exhaustive extends booleantrueWhether to throw if no handler matches (default: true)
ParameterTypeDefault valueDescription
exhaustivebooleantrueIf true, throws when no handler matches and no fallback is present.
casesObjCundefinedObject mapping keys to handler functions.
keystringundefinedKey to match and dispatch to the corresponding handler.
paramsany[]undefinedParameters to pass to the handler function.

A

The result of the matched handler, or undefined if not exhaustive and no match.

match(true, { foo: fn, bar: fn, _: fallback }, "foo", ...params);