matchWithString
matchWithString() performs exhaustive pattern matching on a string literal or a union of string literals. Every possible value must have exactly one handler, guaranteeing that no case is left unprocessed. The selected callback receives the literal value corresponding to its key, correctly narrowed by TypeScript.
Interactive example
Syntax
Classic signature
typescript
function matchWithString<Input extends string, Matcher>(
input: Input,
matcher: Matcher
): ReturnType<Matcher[keyof Matcher]>Curried signature
typescript
function matchWithString<Input extends string, Matcher>(
matcher: Matcher
): (input: Input) => ReturnType<Matcher[keyof Matcher]>Parameters
input: a string literal or a union of string literals. Broadstringvalues are rejected.matcher: an exhaustive object whose keys are exactly the input literals. Each handler receives its corresponding narrowed literal.
Return value
The selected handler result. Its static type is the union of all handler return types.
See also
matchWithNumber- Numeric equivalent.match- General-purpose pattern matching.
