Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
ts-pattern
Advanced tools
ts-pattern is a pattern matching library for TypeScript that allows for expressive and type-safe pattern matching. It provides a way to handle complex data structures and control flow in a concise and readable manner.
Basic Pattern Matching
This feature allows you to match a value against multiple patterns and execute corresponding actions. The 'otherwise' method provides a default case.
const { match } = require('ts-pattern');
const value = 2;
const result = match(value)
.with(1, () => 'one')
.with(2, () => 'two')
.with(3, () => 'three')
.otherwise(() => 'unknown');
console.log(result); // Output: 'two'
Nested Pattern Matching
This feature allows you to match nested objects and structures, making it easier to handle complex data.
const { match } = require('ts-pattern');
const data = { type: 'user', user: { name: 'Alice', age: 30 } };
const result = match(data)
.with({ type: 'user', user: { name: 'Alice' } }, () => 'Hello Alice')
.with({ type: 'user', user: { age: 30 } }, () => 'User is 30 years old')
.otherwise(() => 'Unknown user');
console.log(result); // Output: 'Hello Alice'
Guard Functions
Guard functions allow you to use custom logic to determine if a pattern matches, providing greater flexibility.
const { match, when } = require('ts-pattern');
const value = 10;
const result = match(value)
.with(when(x => x > 5), () => 'greater than 5')
.with(when(x => x <= 5), () => '5 or less')
.otherwise(() => 'unknown');
console.log(result); // Output: 'greater than 5'
match-ts is another TypeScript pattern matching library. It offers similar functionality to ts-pattern but with a different API design. It focuses on providing a more functional programming style.
typescript-pattern-matching is a library that provides pattern matching capabilities for TypeScript. It is similar to ts-pattern but may have different performance characteristics and API design choices.
fp-ts is a library for functional programming in TypeScript. While it is not solely focused on pattern matching, it provides utilities for handling data structures and control flow in a functional style, which can be used to achieve similar outcomes.
The exhaustive Pattern Matching library for TypeScript with smart type inference.
import { match, P } from 'ts-pattern';
type Data =
| { type: 'text'; content: string }
| { type: 'img'; src: string };
type Result =
| { type: 'ok'; data: Data }
| { type: 'error'; error: Error };
const result: Result = ...;
const html = match(result)
.with({ type: 'error' }, () => `<p>Oups! An error occured</p>`)
.with({ type: 'ok', data: { type: 'text' } }, (res) => `<p>${res.data.content}</p>`)
.with({ type: 'ok', data: { type: 'img', src: P.select() } }, (src) => `<img src=${src} />`)
.exhaustive();
Write better and safer conditions. Pattern matching lets you express complex conditions in a single, compact expression. Your code becomes shorter and more readable. Exhaustiveness checking ensures you havenβt forgotten any possible case.
Animation by @nicoespeon
.exhaustive()
.isMatching
.P._
, P.string
, P.number
, etc.P.select(name?)
function.Pattern Matching is a code-branching technique coming from functional programming languages, which lets you scrutinize the structure of values in a declarative way. It has proven itself to be less verbose and more powerful than imperative alternatives (if/else/switch statements), especially when branching on complex data structures or on several values.
Pattern Matching is implemented in Haskell, Rust, Swift, Elixir and many other languages. There is a tc39 proposal to add Pattern Matching to the EcmaScript specification, but it is still in stage 1 and isn't likely to land before several years. Luckily, pattern matching can be implemented in userland. ts-pattern
Provides a typesafe pattern matching implementation that you can start using today.
Read the introduction blog post: Bringing Pattern Matching to TypeScript π¨ Introducing TS-Pattern
Via npm
npm install ts-pattern
Via yarn
yarn add ts-pattern
Note: TS-Pattern assumes Strict Mode is enabled in your tsconfig.json
file.
ts-pattern | TypeScript v4.5+ | TypeScript v4.2+ | TypeScript v4.1+ |
---|---|---|---|
v4.x (Docs) (Migration Guide) | β | β | β |
v3.x (Docs) | β | β | β οΈ |
v2.x (Docs) | β | β | β |
.with()
.P.when
Guard DemoP.not
Pattern DemoP.select
Pattern DemoP.union
Pattern DemoAs an example, we are going to create a state reducer for a frontend application fetching some data using an HTTP request.
Our application can be in four different states: idle
, loading
,
success
and error
. Depending on which state we are in, some events
can occur. Here are all the possible types of event our application
can respond to: fetch
, success
, error
and cancel
.
I use the word event
but you can replace it with action
if you are used
to Redux's terminology.
type State =
| { status: 'idle' }
| { status: 'loading'; startTime: number }
| { status: 'success'; data: string }
| { status: 'error'; error: Error };
type Event =
| { type: 'fetch' }
| { type: 'success'; data: string }
| { type: 'error'; error: Error }
| { type: 'cancel' };
Even though our application can handle 4 events, only a subset of these
events make sense for each given state. For instance we can only cancel
a request if we are currently in the loading
state.
To avoid unwanted state changes that could lead to bugs, we want to create
a reducer function that matches on both the state and the event
and return a new state.
This is a case where match
really shines. Instead of writing nested
switch statements, we can do that in a very expressive way:
import { match, P } from 'ts-pattern';
const reducer = (state: State, event: Event): State =>
match<[State, Event], State>([state, event])
.with(
[{ status: 'loading' }, { type: 'success' }],
([, event]) => ({
status: 'success',
data: event.data,
})
)
.with(
[{ status: 'loading' }, { type: 'error', error: P.select() }],
(error) => ({
status: 'error',
error,
})
)
.with(
[{ status: P.not('loading') }, { type: 'fetch' }],
() => ({
status: 'loading',
startTime: Date.now(),
})
)
.with(
[
{
status: 'loading',
startTime: P.when((t) => t + 2000 < Date.now()),
},
{ type: 'cancel' },
],
() => ({
status: 'idle',
})
)
.with(P._, () => state)
.exhaustive();
Let's go through this bit by bit:
match
takes a value and returns a builder on which you can add your pattern matching cases.
match<[State, Event], State>([state, event]);
Here we wrap the state and the event objects in an array and we explicitly
specify the type [State, Event]
to make sure it is interpreted as
a Tuple by TypeScript, so we
can match on each value separately.
Most of the time, you don't need to specify the type of input
and output with match<Input, Output>(...)
because match
is able to
infer both of these types.
Then we add a first with
clause:
.with(
[{ status: 'loading' }, { type: 'success' }],
([state, event]) => ({
// `state` is inferred as { status: 'loading' }
// `event` is inferred as { type: 'success', data: string }
status: 'success',
data: event.data,
})
)
The first argument is the pattern: the shape of value you expect for this branch.
The second argument is the handler function: the code branch that will be called if the input value matches the pattern.
The handler function takes the input value as first parameter with its type narrowed down to what the pattern matches.
In the second with
clause, we use the P.select
function:
.with(
[
{ status: 'loading' },
{ type: 'error', error: P.select() }
],
(error) => ({
status: 'error',
error,
})
)
P.select()
lets you extract a piece of your input value and inject it into your handler. It is pretty useful when pattern matching on deep data structures because it avoids the hassle of destructuring your input in your handler.
Since we didn't pass any name to P.select()
, It will inject the event.error
property as first argument to the handler function. Note that you can still access the full input value with its type narrowed by your pattern as second argument of the handler function:
.with(
[
{ status: 'loading' },
{ type: 'error', error: P.select() }
],
(error, stateAndEvent) => {
// error: Error
// stateAndEvent: [{ status: 'loading' }, { type: 'error', error: Error }]
}
)
In a pattern, we can only have a single anonymous selection. If you need to select more properties on your input data structure, you will need to give them names:
.with(
[
{ status: 'success', data: P.select('prevData') },
{ type: 'error', error: P.select('err') }
],
({ prevData, err }) => {
// Do something with (prevData: string) and (err: Error).
}
)
Each named selection will be injected inside a selections
object, passed as first argument to the handler function. Names can be any strings.
If you need to match on everything but a specific value, you can use a P.not(<pattern>)
pattern. it's a function taking a pattern and returning its opposite:
.with(
[{ status: P.not('loading') }, { type: 'fetch' }],
() => ({
status: 'loading',
})
)
P.when()
and guard functionsSometimes, we need to make sure our input value respects a condition that can't be expressed by a pattern. For example, imagine you need to check if a number is positive. In these cases, we can use guard functions: functions taking a value and returning a boolean
.
With ts-pattern
there are two options to use a guard function:
P.when(<guard function>)
inside your pattern.with(...)
.with(
[
{
status: 'loading',
startTime: P.when((t) => t + 2000 < Date.now()),
},
{ type: 'cancel' },
],
() => ({
status: 'idle',
})
)
.with(...)
.with
optionally accepts a guard function as second parameter, between
the pattern
and the handler
callback:
.with(
[{ status: 'loading' }, { type: 'cancel' }],
([state, event]) => state.startTime + 2000 < Date.now(),
() => ({
status: 'idle'
})
)
This pattern will only match if the guard function returns true
.
P._
wildcardP._
will match any value. You can use it either at the top level, or within another pattern.
.with(P._, () => state)
// You could also use it inside another pattern:
.with([P._, P._], () => state)
// at any level:
.with([P._, { type: P._ }], () => state)
.exhaustive();
.exhaustive()
executes the pattern matching expression, and returns the result. It also enables exhaustiveness checking, making sure we don't forget any possible case in our input value. This extra type safety is very nice because forgetting a case is an easy mistake to make, especially in an evolving code-base.
Note that exhaustive pattern matching is optional. It comes with the trade-off of having longer compilation times because the type checker has more work to do.
Alternatively you can use .otherwise()
, which takes an handler function returning a default value. .otherwise(handler)
is equivalent to .with(P._, handler).exhaustive()
.
.otherwise(() => state);
If you don't want to use .exhaustive()
and also don't want to provide a default value with .otherwise()
, you can use .run()
instead:
.run();
It's just like .exhaustive()
, but it's unsafe and might throw runtime error if no branch matches your input value.
As you may know, switch
statements allow handling several cases with
the same code block:
switch (type) {
case 'text':
case 'span':
case 'p':
return 'text';
case 'btn':
case 'button':
return 'button';
}
Similarly, ts-pattern lets you pass several patterns to .with()
and if
one of these patterns matches your input, the handler function will be called:
const sanitize = (name: string) =>
match(name)
.with('text', 'span', 'p', () => 'text')
.with('btn', 'button', () => 'button')
.otherwise(() => name);
sanitize('span'); // 'text'
sanitize('p'); // 'text'
sanitize('button'); // 'button'
As you might expect, this also works with more complex patterns than strings and exhaustiveness checking works as well.
match
match(value);
Create a Match
object on which you can later call .with
, .when
, .otherwise
and .run
.
function match<TInput, TOutput>(input: TInput): Match<TInput, TOutput>;
input
.with
match(...)
.with(pattern, [...patterns], handler)
function with(
pattern: Pattern<TInput>,
handler: (selections: Selections<TInput>, value: TInput) => TOutput
): Match<TInput, TOutput>;
// Overload for multiple patterns
function with(
pattern1: Pattern<TInput>,
...patterns: Pattern<TInput>[],
// no selection object is provided when using multiple patterns
handler: (value: TInput) => TOutput
): Match<TInput, TOutput>;
// Overload for guard functions
function with(
pattern: Pattern<TInput>,
when: (value: TInput) => unknown,
handler: (
selection: Selection<TInput>,
value: TInput
) => TOutput
): Match<TInput, TOutput>;
pattern: Pattern<TInput>
handler
, the with
clause will match if one of the patterns matches.when: (value: TInput) => unknown
TInput
might be narrowed to a more precise type using the pattern
.handler: (value: TInput, selections: Selections<TInput>) => TOutput
match
case must return values of the same type, TOutput
.TInput
might be narrowed to a more precise type using the pattern
.selections
is an object of properties selected from the input with the select
function..when
match(...)
.when(predicate, handler)
function when(
predicate: (value: TInput) => unknown,
handler: (value: TInput) => TOutput
): Match<TInput, TOutput>;
predicate: (value: TInput) => unknown
handler: (value: TInput) => TOutput
match
case must return values of the same type, TOutput
..exhaustive
match(...)
.with(...)
.exhaustive()
Runs the pattern-matching expression and returns its result. It also enables exhaustiveness checking, making sure at compile time that we have handled all possible cases.
function exhaustive(): IOutput;
type Permission = 'editor' | 'viewer';
type Plan = 'basic' | 'pro';
const fn = (org: Plan, user: Permission) =>
match([org, user] as const)
.with(['basic', 'viewer'], () => {})
.with(['basic', 'editor'], () => {})
.with(['pro', 'viewer'], () => {})
// Fails with `NonExhaustiveError<['pro', 'editor']>`
// because the `['pro', 'editor']` case isn't handled.
.exhaustive();
const fn2 = (org: Plan, user: Permission) =>
match([org, user] as const)
.with(['basic', 'viewer'], () => {})
.with(['basic', 'editor'], () => {})
.with(['pro', 'viewer'], () => {})
.with(['pro', 'editor'], () => {})
.exhaustive(); // Works!
.otherwise
match(...)
.with(...)
.otherwise(defaultHandler)
Runs the pattern-matching expression with a default handler which will be called if no previous .with()
clause match the input value, and returns the result.
function otherwise(defaultHandler: (value: TInput) => TOutput): TOutput;
defaultHandler: (value: TInput) => TOutput
default:
case of switch
statements.match
case must return values of the same type, TOutput
..run
match(...)
.with(...)
.run()
Runs the pattern-matching expression and returns its result. It throws an error at run time if no match was found, same as exhaustive()
. However, unlike .exhaustive()
, exhaustiveness is not checked at compile time, meaning the type checker will not verify that all possible cases are covered.
function run(): TOutput;
isMatching
if (isMatching(pattern, value)) {
...
}
isMatching
is a type guard function which checks if a pattern matches a given value. It is curried, which means it can be used in two ways.
With a single argument:
import { isMatching, P } from 'ts-pattern';
const isBlogPost = isMatching({
title: P.string,
description: P.string,
});
if (isBlogPost(value)) {
// value: { title: string, description: string }
}
With two arguments:
const blogPostPattern = {
title: P.string,
description: P.string,
};
if (isMatching(blogPostPattern, value)) {
// value: { title: string, description: string }
}
export function isMatching<p extends Pattern<any>>(
pattern: p
): (value: any) => value is InvertPattern<p>;
export function isMatching<p extends Pattern<any>>(
pattern: p,
value: any
): value is InvertPattern<p>;
pattern: Pattern<any>
value?: any
isMatching
will return a boolean telling us whether the pattern matches the value or not.isMatching
will return another type guard function taking a value and returning a boolean which tells us whether the pattern matches the value or not.A pattern is a description of the expected shape of your input value.
Patterns can be regular JavaScript values ("some string"
, 10
, true
, ...), data structures (objects, arrays, ...), wildcards (P._
, P.string
, P.number
, ...), or special matcher functions (P.not
,
P.when
, P.select
, ...).
All wildcards and matcher functions can be imported either as Pattern
or as P
from the ts-pattern
module.
import { match, Pattern } from 'ts-pattern';
const toString = (value: unknown): string =>
match(value)
.with(Pattern.string, (str) => str)
.with(Pattern.number, (num) => num.toFixed())
.with(Pattern.boolean, (bool) => `${bool}`)
.otherwise(() => 'Unknown');
Or
import { match, P } from 'ts-pattern';
const toString = (value: unknown): string =>
match(value)
.with(P.string, (str) => str)
.with(P.number, (num) => num.toFixed())
.with(P.boolean, (bool) => `${bool}`)
.otherwise(() => 'Unknown');
If your input isn't typed, (if it's a any
or a unknown
), you are free to use any possible pattern. Your handler will infer the input type from the shape of your pattern.
Literals are primitive JavaScript values, like numbers
, strings
, booleans
, bigints
, symbols
, null
, undefined
, or NaN
.
import { match } from 'ts-pattern';
const input: unknown = 2;
const output = match(input)
.with(2, () => 'number: two')
.with(true, () => 'boolean: true')
.with('hello', () => 'string: hello')
.with(undefined, () => 'undefined')
.with(null, () => 'null')
.with(NaN, () => 'number: NaN')
.with(20n, () => 'bigint: 20n')
.otherwise(() => 'something else');
console.log(output);
// => 'number: two'
P._
wildcardThe 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
wildcardThe 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
wildcardThe P.number
pattern will match any value of type number
.
import { match, P } from 'ts-pattern';
const input = 2;
const output = match<number | string>(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
wildcardThe P.boolean
pattern will match any value of type boolean
.
import { match, P } from 'ts-pattern';
const input = true;
const output = match<number | string | boolean>(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
wildcardThe 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<number | null | undefined>(input)
.with(P.number, () => 'it is a number!')
.with(P.nullish, () => 'it is either null or undefined!')
.with(null, () => 'it is null!')
.with(undefined, () => 'it is undefined!')
.exhaustive();
console.log(output);
// => 'it is either null or undefined!'
P.bigint
wildcardThe P.bigint
pattern will match any value of type bigint
.
import { match, P } from 'ts-pattern';
const input = 20000000n;
const output = match<bigint | null>(input)
.with(P.bigint, () => 'it is a bigint!')
.otherwise(() => '?');
console.log(output);
// => 'it is a bigint!'
P.symbol
wildcardThe P.symbol
pattern will match any value of type symbol
.
import { match, P } from 'ts-pattern';
const input = Symbol('some symbol');
const output = match<symbol | null>(input)
.with(P.symbol, () => 'it is a symbol!')
.otherwise(() => '?');
console.log(output);
// => 'it is a symbol!'
Patterns can be objects containing sub-patterns. An object pattern will match If and only if the input value is an object, contains all properties the pattern defines and each property matches the corresponding sub-pattern.
import { match } from 'ts-pattern';
type Input =
| { type: 'user'; name: string }
| { type: 'image'; src: string }
| { type: 'video'; seconds: number };
let input: Input = { type: 'user', name: 'Gabriel' };
const output = match(input)
.with({ type: 'image' }, () => 'image')
.with({ type: 'video', seconds: 10 }, () => 'video of 10 seconds.')
.with({ type: 'user' }, ({ name }) => `user of name: ${name}`)
.otherwise(() => 'something else');
console.log(output);
// => 'user of name: Gabriel'
In TypeScript, Tuples are arrays with a fixed number of elements which can be of different types. You can pattern-match on tuples using a tuple pattern. A tuple pattern will match if the input value is an array of the same length, and each item match the corresponding sub-pattern.
import { match, P } from 'ts-pattern';
type Input =
| [number, '+', number]
| [number, '-', number]
| [number, '*', number]
| ['-', number];
const input: Input = [3, '*', 4];
const output = match(input)
.with([P._, '+', P._], ([x, , y]) => x + y)
.with([P._, '-', P._], ([x, , y]) => x - y)
.with([P._, '*', P._], ([x, , y]) => x * y)
.with(['-', P._], ([, x]) => -x)
.otherwise(() => NaN);
console.log(output);
// => 12
P.array
patternsTo match on arrays of unknown size, you can use P.array(subpattern)
.
It takes a sub-pattern, and will match if all elements in the input
array match this sub-pattern.
import { match, P } from 'ts-pattern';
type Input = { title: string; content: string }[];
let input: Input = [
{ title: 'Hello world!', content: 'This is a very interesting content' },
{ title: 'Bonjour!', content: 'This is a very interesting content too' },
];
const output = match(input)
.with(
P.array({ title: P.string, content: P.string }),
(posts) => 'a list of posts!'
)
.otherwise(() => 'something else');
console.log(output);
// => 'a list of posts!'
Patterns can be Sets.
import { match, P } from 'ts-pattern';
type Input = Set<string | number>;
const input: Input = new Set([1, 2, 3]);
const output = match(input)
.with(new Set([1, 'hello']), (set) => `Set contains 1 and 'hello'`)
.with(new Set([1, 2]), (set) => `Set contains 1 and 2`)
.with(new Set([P.string]), (set) => `Set contains only strings`)
.with(new Set([P.number]), (set) => `Set contains only numbers`)
.otherwise(() => '');
console.log(output);
// => 'Set contains 1 and 2'
If a Set pattern contains one single wildcard pattern, it will match if each value in the input set match the wildcard.
If a Set pattern contains several values, it will match if the input Set contains each of these values.
Patterns can be Maps. They match if the input is a Map, and if each value match the corresponding sub-pattern.
import { match, P } from 'ts-pattern';
type Input = Map<string, string | number>;
const input: Input = new Map([
['a', 1],
['b', 2],
['c', 3],
]);
const output = match(input)
.with(new Map([['b', 2]]), (map) => `map.get('b') is 2`)
.with(new Map([['a', P.string]]), (map) => `map.get('a') is a string`)
.with(
new Map([
['a', P.number],
['c', P.number],
]),
(map) => `map.get('a') and map.get('c') are number`
)
.otherwise(() => '');
console.log(output);
// => 'map.get('b') is 2'
P.when
patternsP.when
lets you define your own logic to check if the pattern should match or not.
If the predicate
function given to when returns a truthy value, then the pattern
will match for this input.
Note that you can narrow down the type of your input by providing a
Type Guard function to P.when
.
import { match, P } from 'ts-pattern';
type Input = { score: number };
const output = match({ score: 10 })
.with(
{
score: P.when((score): score is 5 => score === 5),
},
(input) => 'π' // input is inferred as { score: 5 }
)
.with({ score: P.when((score) => score < 5) }, () => 'π')
.with({ score: P.when((score) => score > 5) }, () => 'π')
.run();
console.log(output);
// => 'π'
P.not
patternsP.not
lets you match on everything but a specific value.
it's a function taking a pattern and returning the opposite pattern.
import { match, P } from 'ts-pattern';
type Input = boolean | number;
const toNumber = (input: Input) =>
match(input)
.with(P.not(P.boolean), (n) => n) // n: number
.with(true, () => 1)
.with(false, () => 0)
.run();
console.log(toNumber(2));
// => 2
console.log(toNumber(true));
// => 1
P.select
patternsP.select
lets you pick a piece of your input data-structure
and injects it in your handler function.
It's especially useful when pattern matching on deep data structure to avoid the hassle of destructuring it in the handler function.
Selections can be either named (with P.select('someName')
) or anonymous (with P.select()
).
You can have only one anonymous selection by pattern, and the selected value will be directly inject in your handler as first argument:
import { match, P } from 'ts-pattern';
type Input =
| { type: 'post'; user: { name: string } }
| { ... };
const input: Input = { type: 'post', user: { name: 'Gabriel' } }
const output = match(input)
.with(
{ type: 'post', user: { name: P.select() } },
username => username // username: string
)
.otherwise(() => 'anonymous');
console.log(output);
// => 'Gabriel'
If you need to select several things inside your input data structure, you can name your selections by giving a string to P.select(<name>)
. Each selection will be passed as first argument to your handler in an object.
import { match, P } from 'ts-pattern';
type Input =
| { type: 'post'; user: { name: string }, content: string }
| { ... };
const input: Input = { type: 'post', user: { name: 'Gabriel' }, content: 'Hello!' }
const output = match(input)
.with(
{ type: 'post', user: { name: P.select('name') }, content: P.select('body') },
({ name, body }) => `${name} wrote "${body}"`
)
.otherwise(() => '');
console.log(output);
// => 'Gabriel wrote "Hello!"'
You can also pass a sub-pattern to P.select
if you want it to only
select values which match this sub-pattern:
type User = { age: number; name: string };
type Post = { body: string };
type Input = { author: User; content: Post };
declare const input: Input;
const output = match(input)
.with(
{
author: P.select({ age: P.when((age) => age > 18) }),
},
(author) => author // author: User
)
.with(
{
author: P.select('author', { age: P.when((age) => age > 18) }),
content: P.select(),
},
({ author, content }) => author // author: User, content: Post
)
.otherwise(() => 'anonymous');
P.optional
patternsP.optional(subpattern)
lets you annotate a key in an object pattern as being optional,
but if it is defined it should match a given sub-pattern.
import { match, P } from 'ts-pattern';
type Input = { key?: string | number };
const output = match(input)
.with({ key: P.optional(P.string) }, (a) => {
return a.key; // string | undefined
})
.with({ key: P.optional(P.number) }, (a) => {
return a.key; // number | undefined
})
.exhaustive();
P.instanceOf
patternsThe P.instanceOf
function lets you build a pattern to check if
a value is an instance of a class:
import { match, P } from 'ts-pattern';
class A {
a = 'a';
}
class B {
b = 'b';
}
type Input = { value: A | B };
const input: Input = { value: new A() };
const output = match(input)
.with({ value: P.instanceOf(A) }, (a) => {
return 'instance of A!';
})
.with({ value: P.instanceOf(B) }, (b) => {
return 'instance of B!';
})
.exhaustive();
console.log(output);
// => 'instance of A!'
P.union
patternsP.union(...subpatterns)
lets you test several patterns and will match if
one of these patterns do. It's particularly handy when you want to handle
some cases of a union type in the same code branch:
import { match, P } from 'ts-pattern';
type Input =
| { type: 'user'; name: string }
| { type: 'org'; name: string }
| { type: 'text'; content: string }
| { type: 'img'; src: string };
declare const input: Input;
const output = match(input)
.with({ type: P.union('user', 'org') }, (userOrOrg) => {
// userOrOrg: User | Org
return userOrOrg.name;
})
.otherwise(() => '');
P.intersection
patternsP.intersection(...subpatterns)
lets you ensure that the input matches
all sub-patterns passed as parameters.
class A {
constructor(public foo: 'bar' | 'baz') {}
}
class B {
constructor(public str: string) {}
}
type Input = { prop: A | B };
declare const input: Input;
const output = match(input)
.with(
{ prop: P.intersection(P.instanceOf(A), { foo: 'bar' }) },
({ prop }) => prop.foo // prop: A & { foo: 'bar' }
)
.with(
{ prop: P.intersection(P.instanceOf(A), { foo: 'baz' }) },
({ prop }) => prop.foo // prop: A & { foo: 'baz' }
)
.otherwise(() => '');
P.infer
P.infer<typeof somePattern>
lets you infer a type of value from a type of pattern.
It's particularly useful when validating an API response.
const postPattern = {
title: P.string,
content: P.string,
likeCount: P.number,
author: {
name: P.string,
},
};
type Post = P.infer<typeof postPattern>;
// posts: Post[]
const posts = await fetch(someUrl)
.then((res) => res.json())
.then((res: unknown): Post[] =>
isMatching({ data: P.array(postPattern) }, res) ? res.data : []
);
P.Pattern
P.Pattern<T>
is the type of all possible pattern for a generic type T
.
type User = { name: string; age: number };
const userPattern: Pattern<User> = {
name: 'Alice',
};
TS-Pattern takes advantage of some of the most advanced features of the type system to narrow the input type using the current pattern. It is also able to accurately know if you have handled all cases, even when matching on complex data-structures.
Here are some examples of TS-Pattern's inference features.
If you pattern-match on a union type with a discriminant property, TS-Pattern will use this discriminant to narrow the type of input.
type Text = { type: 'text'; data: string };
type Img = { type: 'img'; data: { src: string; alt: string } };
type Video = { type: 'video'; data: { src: string; format: 'mp4' | 'webm' } };
type Content = Text | Img | Video;
const formatContent = (content: Content): string =>
match(content)
.with({ type: 'text' }, (text /* : Text */) => '<p>...</p>')
.with({ type: 'img' }, (img /* : Img */) => '<img ... />')
.with({ type: 'video' }, (video /* : Video */) => '<video ... />')
.with(
{ type: 'img' },
{ type: 'video' },
(video /* : Img | Video */) => 'img or video'
)
.with(
{ type: P.union('img', 'video') },
(video /* : Img | Video */) => 'img or video'
)
.exhaustive();
If you use P.select
, TS-Pattern will pick up the type of the property you selected, and will inferyour handler's type accordingly.
const formatContent = (content: Content): string =>
match(content)
.with(
{ type: 'text', data: P.select() },
(content /* : string */) => '<p>...</p>'
)
.with(
{ type: 'video', data: { format: P.select() } },
(format /* : 'mp4' | 'webm' */) => '<video ... />'
)
.with(
{ type: P.union('img', 'video'), data: P.select() },
(data /* : Img['data'] | Video['data'] */) => 'img or video'
)
.exhaustive();
If the function given to P.when
is a Type Guard, TS-Pattern will use the type guard's return type to narrow the input.
const isString = (x: unknown): x is string => typeof x === 'string';
const isNumber = (x: unknown): x is number => typeof x === 'number';
const fn = (input: { id: number | string }) =>
match(input)
.with({ id: P.when(isString) }, (narrowed /* : { id: string } */) => 'yes')
.with({ id: P.when(isNumber) }, (narrowed /* : { id: number } */) => 'yes')
.exhaustive();
If your data structure contains several union types, you can pattern-match on several of them with a single pattern. TS-Pattern will keep track of the cases which have been handled and those which haven't, so you never forget to handle a case.
type Permission = 'editor' | 'viewer';
type Plan = 'basic' | 'pro';
const fn = (org: Plan, user: Permission): string =>
match([org, user] as const)
.with(['basic', 'viewer'], () => {})
.with(['basic', 'editor'], () => {})
.with(['pro', 'viewer'], () => {})
// Fails with `NonExhaustiveError<['pro', 'editor']>`
// because the `['pro', 'editor']` case isn't handled.
.exhaustive();
Check out π Type-Level TypeScript, an online course to learn how to take full advantage of the most advanced features of TypeScript!
This library has been heavily inspired by this great article by Wim Jongeneel: Pattern Matching in TypeScript with Record and Wildcard Patterns. It made me realize pattern matching could be implemented in userland and we didn't have to wait for it to be added to the language itself. I'm really grateful for that π
FAQs
The exhaustive Pattern Matching library for TypeScript.
The npm package ts-pattern receives a total of 1,338,259 weekly downloads. As such, ts-pattern popularity was classified as popular.
We found that ts-pattern demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.