OSS Tanbou

TS-Pattern — express TypeScript branching with patterns and exhaustiveness checks

About these scores

OSS scale score is an unbounded metric that log-compresses and weights Stars, Watchers, Forks, and Contributors. Discovery score is the current OSS scale score minus the score at discovery. Update pace is commits in the last 30 days, growth momentum is the OSS scale score difference within the recent observation window, and OSS health is a 0–100 rating based on available recency, Community Health, and release data.

Stars
15,167
Primary language
TypeScript
License
MIT
Repository last updated
Sep 11, 2026

Overview

TS-Pattern matches TypeScript unions, objects, arrays, tuples, and primitive values through declarative patterns. Cases are added with `.with()`, and `.exhaustive()` can detect missing branches at compile time.

Features and best fit

Based on official documentation; not hands-on tested · Content checked:

Match directly on object and union shapes

Patterns can describe nested objects, tuples, and primitive values while handlers receive appropriately narrowed input types.

Sources: [2]

Use `.exhaustive()` to catch missing cases during type checking

Discriminated unions can be checked so every variant is handled, including when new variants are added later.

Sources: [2][3]

For complex state-machine and union branching

It fits reducers, API-result handling, and domain models where nested switch or if/else logic becomes difficult to maintain.

Sources: [2]

Measure compile-time cost of advanced type inference

Runtime output is small, but pattern inference is handled by TypeScript. Large numbers of complex patterns may justify monitoring type-check performance.

Sources: [2][3]

Official sources

  1. [1]gvergnaud/ts-pattern repository(2026-09-21)
  2. [2]TS-Pattern README(2026-09-21)
  3. [3]ts-pattern package(2026-09-21)
  4. [4]TS-Pattern MIT license(2026-09-21)
Supplemental curator note

The current package is 5.9.0. TS-Pattern is more than switch syntax: it matches nested objects, tuples, and unions while using TypeScript narrowing and exhaustiveness checks. Because it relies on advanced type-level computation, very large pattern-heavy codebases should also monitor type-check performance.

Try it in 3 steps

  1. 1

    Install TS-Pattern

    Add the package using the npm command from the README.

    npm install ts-pattern
  2. 2

    Match a discriminated union

    Express union branches as structural patterns.

    match(result).with({ type: 'error' }, handleError).with({ type: 'ok' }, handleOk)
  3. 3

    Enable exhaustiveness checking

    Verify that TypeScript reports any unhandled union case.

    .exhaustive()
Check the official README

Growth

Growth trends · Last 30 days

15,167 Stars

Trend data is still being collected.

Development activity

Last 90 days · weekly

Commits (last 30 days)
0
Open PRs
24

Development activity is still being collected.

Built with

Categories and tags

Categories

GitHub data

GitHub dataView detailed GitHub data

GitHub Topics

  • pattern-matching
  • typescript
  • ts
  • pattern
  • matching
  • inference
  • type-inference
  • exhaustive
  • conditions
  • branching
  • javascript
Stars
15,167
Forks
172
Watchers
31
Open issues
53
Contributors
26
Primary language
TypeScript
License
MIT
Repository last updated
Sep 11, 2026
Write a related article

Share a guide or use case for this OSS in Markdown. Articles are published after administrator approval.

Report incorrect information

Tell us if any listing information is incorrect or outdated.