Match
The match function provides a powerful way to handle different types of input and return corresponding outputs based on the input type, like a type-safe switch statement.
Case Record API
The simplest way to define a matcher is with ArkType definition strings as keys with corresponding handlers as values:
In this example, sizeOf is a matcher that takes a string, array, number, or bigint as input. It returns the length of strings and arrays, and the value of numbers and bigints.
default accepts one of 4 values:
"assert": acceptunknown, throw if none of the cases match"never": accept an input based on inferred cases, throw if none match"reject": acceptunknown, returnArkErrorsif none of the cases match(data: In) => unknown: handle data not matching other cases directly
Cases will be checked in the order they are specified, either as object literal keys or via chained methods.
Fluent API
The match function also provides a fluent API. This can be convenient for non-string-embeddable definitions: