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.
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.
Official sources
- [1]gvergnaud/ts-pattern repository(2026-09-21)
- [2]TS-Pattern README(2026-09-21)
- [3]ts-pattern package(2026-09-21)
- [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
Install TS-Pattern
Add the package using the npm command from the README.
npm install ts-pattern - 2
Match a discriminated union
Express union branches as structural patterns.
match(result).with({ type: 'error' }, handleError).with({ type: 'ok' }, handleOk) - 3
Enable exhaustiveness checking
Verify that TypeScript reports any unhandled union case.
.exhaustive()
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
Related information
Write a related articleShare a guide or use case for this OSS in Markdown. Articles are published after administrator approval.
Explore next
- Visual Studio Code192,752 Stars
1 shared tag(s) · 1 shared category(s) · Same language
The MIT-licensed Code - OSS repository that forms the open foundation of Visual Studio Code.
TypeScript - Supabase110,464 Stars
1 shared tag(s) · 1 shared category(s) · Same language
An open Postgres development platform combining Database, Auth, APIs, Realtime, Storage, and Functions.
TypeScript - Pi107,901 Stars
1 shared tag(s) · 1 shared category(s) · Same language
A minimal and extensible harness for AI coding agents.
TypeScript - Tailwind CSS97,630 Stars
1 shared tag(s) · 1 shared category(s) · Same language
A CSS framework for building custom interfaces by composing utility classes.
TypeScript - Playwright96,435 Stars
1 shared tag(s) · 1 shared category(s) · Same language
A web automation platform spanning Chromium, Firefox, and WebKit E2E tests, scripts, a coding-agent CLI, and MCP through one API.
TypeScript
Report incorrect information
Tell us if any listing information is incorrect or outdated.