Skip to Content
Wildcards

Last Updated: 3/6/2026


Wildcard Patterns

P._ wildcard

The P._ pattern will match any value. You can also use P.any, which is an alias to P._.

import { match, P } from 'ts-pattern'; const input = 'hello'; const output = match(input) .with(P._, () => 'It will always match') // OR .with(P.any, () => 'It will always match') .otherwise(() => 'This string will never be used'); console.log(output); // => 'It will always match'

P.string wildcard

The P.string pattern will match any value of type string.

import { match, P } from 'ts-pattern'; const input = 'hello'; const output = match(input) .with('bonjour', () => 'Won't match') .with(P.string, () => 'it is a string!') .exhaustive(); console.log(output); // => 'it is a string!'

P.number wildcard

The P.number pattern will match any value of type number.

import { match, P } from 'ts-pattern'; const input = 2; const output = match(input) .with(P.string, () => 'it is a string!') .with(P.number, () => 'it is a number!') .exhaustive(); console.log(output); // => 'it is a number!'

P.boolean wildcard

The P.boolean pattern will match any value of type boolean.

import { match, P } from 'ts-pattern'; const input = true; const output = match(input) .with(P.string, () => 'it is a string!') .with(P.number, () => 'it is a number!') .with(P.boolean, () => 'it is a boolean!') .exhaustive(); console.log(output); // => 'it is a boolean!'

P.nullish wildcard

The P.nullish pattern will match any value of type null or undefined.

Even though null and undefined can be used as literal patterns, sometimes they appear in a union together (e.g. null | undefined | string) and you may want to treat them as equivalent using P.nullish.

import { match, P } from 'ts-pattern'; const input = null; const output = match(input) .with(P.number, () => 'it is a number!') .with(P.nullish, () => 'it is either null or undefined!') .exhaustive(); console.log(output); // => 'it is either null or undefined!'

P.nonNullable wildcard

The P.nonNullable pattern will match any value except null or undefined.

import { match, P } from 'ts-pattern'; const input = null; const output = match(input) .with(P.nonNullable, () => 'it is a number!') .otherwise(() => 'it is either null or undefined!'); console.log(output); // => 'it is either null or undefined!'

P.bigint wildcard

The P.bigint pattern will match any value of type bigint.

import { match, P } from 'ts-pattern'; const input = 20000000n; const output = match(input) .with(P.bigint, () => 'it is a bigint!') .otherwise(() => '?'); console.log(output); // => 'it is a bigint!'

P.symbol wildcard

The P.symbol pattern will match any value of type symbol.

import { match, P } from 'ts-pattern'; const input = Symbol('some symbol'); const output = match(input) .with(P.symbol, () => 'it is a symbol!') .otherwise(() => '?'); console.log(output); // => 'it is a symbol!'