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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
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 boolean | true | Whether to throw if no handler matches (default: true) |
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
exhaustive | boolean | true | If true, throws when no handler matches and no fallback is present. |
casesObj | C | undefined | Object mapping keys to handler functions. |
key | string | undefined | Key to match and dispatch to the corresponding handler. |
…params | any[] | undefined | Parameters to pass to the handler function. |
Returns
Section titled “Returns”A
The result of the matched handler, or undefined if not exhaustive and no match.
Example
Section titled “Example”match(true, { foo: fn, bar: fn, _: fallback }, "foo", ...params);