isMatch
isMatch() imperatively tests whether a value matches a pattern and returns a type guard (value is ...). Ideal for simple conditions or to refine the type before entering a pipeline.
Interactive example
Syntax
Classic signature
typescript
function isMatch<
GenericInput extends AnyValue,
const GenericPattern extends Pattern<GenericInput>
>(
input: GenericInput,
pattern: FixDeepFunctionInfer<Pattern<GenericInput>, GenericPattern>
): input is ForcePredicate<GenericInput, ComplexMatchedValue<GenericInput, PatternValue<GenericPattern>>>;Curried signature
typescript
function isMatch<
GenericInput extends AnyValue,
const GenericPattern extends Pattern<GenericInput>
>(
pattern: FixDeepFunctionInfer<Pattern<GenericInput>, GenericPattern>
): (
input: GenericInput
) => input is ForcePredicate<GenericInput, ComplexMatchedValue<GenericInput, PatternValue<GenericPattern>>>;PatternValue is inferred from the shape of the pattern (literal, partial object, tuple, predicate, ToolPattern, etc.).
Parameters
input(optional): value to test immediately. Without this argument,isMatchreturns a reusable type guard.pattern: pattern to compare (same possibilities asmatch).
Return value
A boolean type guard: true if the value matches the pattern, and the type is refined accordingly. false otherwise, with no effect on the type.
Best practices
- Use
isMatchin conditions (if,filter,find) to refine your unions before apipeor a richermatch. - Combine
isMatch(P.union(...))to quickly create reusable predicates. - Prefer
match/whenif you need to transform the value rather than only filter it.
