ts-pattern

The exhaustive pattern matching library for TypeScript, with smart type inference.

Library
npm
v5.9.0
15,146stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
62/100Good
Development Activity48
Maintenance44
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture82
Code Quality85
Innovation88
Learning Curve72

ts-pattern brings exhaustive, type-safe pattern matching to TypeScript, a feature borrowed from languages like Rust, OCaml, and Haskell. Its match() API lets developers replace deeply nested if/else and switch statements with declarative pattern-matching expressions that TypeScript’s type checker can verify for exhaustiveness.

The library provides selectors like P.string, P.number, P.array(), P.union(), and P.when() for building complex match patterns, and narrows types automatically within each matched branch, catching unhandled cases at compile time via .exhaustive().

What You Get

  • A match() function with a fluent .with()/.exhaustive()/.otherwise() API for building match expressions
  • A rich pattern selector library (P.string, P.number, P.array, P.union, P.when, P.instanceOf, etc.)
  • Compile-time exhaustiveness checking that fails to build if a case is unhandled
  • Automatic type narrowing within each matched branch based on the pattern shape
  • Support for matching discriminated unions, tuples, nested objects, and arrays

Common Use Cases

  • Replacing deeply nested if/else or switch statements with declarative pattern matching
  • Exhaustively handling all variants of a discriminated union (e.g. a state machine or Redux action type)
  • Matching and destructuring complex nested API response shapes with type-safe narrowing
  • Implementing type-safe reducers or event handlers where every case must be explicitly covered

Under The Hood

Architecture - The library’s core is the match() entry point (src/index.ts) combined with a pattern-building DSL exposed as P (patterns module), which constructs a pattern tree matched against the input value at runtime while TypeScript’s conditional types compute the narrowed branch type at compile time; the .exhaustive() terminal method triggers a compile-time check that all union variants were handled. Tech Stack - Pure TypeScript with zero runtime dependencies, built with a dual ESM/CJS output (dist/index.js and dist/index.cjs) and published to both npm and JSR; benchmarks live in a dedicated benchmarks/ directory to track match performance. Code Quality - The tests/ directory contains around 50 TypeScript test files covering exhaustiveness checking, deep pattern nesting, and type-inference edge cases, run via Jest; docs/ contains a documentation site source alongside examples/ demonstrating real-world matching scenarios. API Design - The fluent match(value).with(pattern, handler).exhaustive() chain reads close to native pattern-matching syntax in ML-family languages, and the type system does the heavy lifting so incorrect handlers are caught at compile time rather than requiring manual runtime checks — a deliberate design choice that trades a small amount of match() call overhead for strong compile-time guarantees.

Used by 17 apps in this directory

TypeScript
76%
AGPL 3.0

Abby

Developer Tools · Product Management

166

Statically typed feature flags, remote config, and A/B testing with framework-native SDKs for TypeScript teams.

View details
30
Repo Health
70
Technical
63
Dependency
Built with
TypeScript76%
MDX18%
Updated 1 years ago
TypeScript
87%
Other

AFFiNE

Productivity · Project Management · Note Taking

72,238

Write, draw, and plan in one infinite canvas — the open-source alternative to Notion and Miro that keeps your data yours.

View details
91
Repo Health
87
Technical
67
Dependency
Built with
TypeScript87%
Updated yesterday
TypeScript
72%
AGPL 3.0

APITable

Low Code Platforms · Databases

15,588

API-first collaborative spreadsheet-database platform that auto-generates REST APIs and lets teams build internal tools, CRMs, and dashboards without code.

View details
74
Repo Health
77
Technical
60
Dependency
Built with
TypeScript72%
Java22%
Updated yesterday
Python
56%
Other

authentik

Authentication · Security

25,385

The self-hosted Identity Provider that replaces Okta, Auth0, and Entra ID with a unified SSO platform supporting SAML, OAuth2/OIDC, LDAP, RADIUS, and WebAuthn.

View details
92
Repo Health
81
Technical
66
Dependency
Built with
Python56%
TypeScript34%
Updated yesterday
Rust
67%
MIT

Bun

Developer Tools

95,895

An all-in-one JavaScript and TypeScript toolkit — one Rust-and-JavaScriptCore binary that replaces Node.js, npm, a bundler, and a test runner with faster equivalents.

View details
92
Repo Health
91
Technical
64
Dependency
Built with
Rust67%
C++19%
Updated yesterday
TypeScript
98%
Apache 2.0

Codebuff

AI Code Assistants

11,750

An open-source AI coding assistant that coordinates specialized agents to edit your codebase from natural language — including Freebuff, a free, ad-supported version powered entirely by open-source models like DeepSeek and Kimi.

View details
85
Repo Health
71
Technical
67
Dependency
Built with
TypeScript98%
Updated yesterday
TypeScript
99%
Apache 2.0

Colanode

Knowledge Management · Team Chat · Collaboration

5,097

Local-first, self-hosted workspace that combines real-time chat, Notion-style pages, and structured databases — all synced via CRDTs so you work offline without losing a keystroke.

View details
47
Repo Health
73
Technical
70
Dependency
Built with
TypeScript99%
Updated 5 months ago
TypeScript
92%
AGPL 3.0

Documenso

Digital Signiture

14,913

Self-hosted, open-source DocuSign alternative with legally binding PDF signatures, multi-party workflows, and a full REST and tRPC API.

View details
93
Repo Health
79
Technical
71
Dependency
Built with
TypeScript92%
Updated today
Clojure
55%
Other

Metabase

Analytics

49,109

The open-source BI platform that lets anyone ask questions and build dashboards without writing SQL — with an embedded analytics SDK and AI-powered query assistant included.

View details
95
Repo Health
84
Technical
65
Dependency
Built with
Clojure55%
TypeScript40%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search