Skip to Content
Match

Last Updated: 3/6/2026


match()

Create a Match object on which you can later call .with, .when, .otherwise and .run.

Signature

function match<TInput, TOutput>(input: TInput): Match<TInput, TOutput>;

Arguments

  • input
    • Required
    • The input value your patterns will be tested against.

Example

import { match } from 'ts-pattern'; const result = match(value) .with(pattern1, handler1) .with(pattern2, handler2) .otherwise(defaultHandler);

Type Parameters

It’s also possible to specify the input and output type explicitly with match<Input, Output>(...), but this is usually unnecessary, as TS-Pattern is able to infer them.

const result = match<string | number, string>(value) .with('hello', () => 'greeting') .with(42, () => 'answer') .otherwise(() => 'unknown');