ts-pattern

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

Library
npm
v5.9.2
15,195 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
79 /100 Good
Development Activity 76
Maintenance 84
Community 56
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
82 /100 Excellent
Architecture 82
Code Quality 85
Innovation 88
Learning Curve 72

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

Elixir
55%
Other

Operately

AI Assistants · Collaboration · Productivity

592

The open source company operating system that unifies OKRs, projects, and team execution with built-in accountability cadences.

View details
86
Repo Health
80
Technical
74
Dependency
Built with
Elixir 55%
TypeScript 44%
Updated 1 weeks ago
TypeScript
99%
Other

Papermark

Digital Signiture · File Storage

9,263

Open-source DocSend alternative with page-by-page analytics, secure data rooms, and custom domains for document sharing.

View details
78
Repo Health
63
Technical
66
Dependency
Built with
TypeScript 99%
Updated 2 months ago
TypeScript
94%
AGPL 3.0

Papra

Bookmarks Archiving

5,587

Self-hosted document archiving with email ingestion, OCR full-text search, and pluggable storage — store once, find anything.

View details
86
Repo Health
82
Technical
69
Dependency
Built with
TypeScript 94%
Updated 1 weeks ago
Python
55%
Other

PostHog

Ab Testing Experimentation · Analytics · Developer Tools

40,397

The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python 55%
TypeScript 36%
Updated 6 days ago
Rust
72%
Apache 2.0

Rivet

AI Agents · Developer Tools

6,326

Stateful actors as a primitive for AI agents, real-time collaboration, and durable execution — with in-memory state, WebSockets, queues, and scheduling built in.

View details
87
Repo Health
81
Technical
64
Dependency
Built with
Rust 72%
TypeScript 25%
Updated 6 days ago
TypeScript
99%
Other

Teable

Databases · No Code Platforms

21,920

A no-code PostgreSQL database with spreadsheet UX, real-time collaboration, and native AI agents — built for teams that outgrow Airtable.

View details
78
Repo Health
76
Technical
62
Dependency
Built with
TypeScript 99%
Updated 1 weeks ago
TypeScript
63%
AGPL 3.0

undb

Databases · No Code Platforms

2,975

Self-hosted no-code database and BaaS that lets you manage structured data visually with multi-database support, formula fields, and auto-generated OpenAPI endpoints.

View details
58
Repo Health
66
Technical
65
Dependency
Built with
TypeScript 63%
Svelte 35%
Updated 1 months ago
TypeScript
96%
GPL 3.0

Wire

Collaboration · Team Chat · Video Conferencing

1,192

Open source end-to-end encrypted messaging for teams and enterprises — self-host for complete control over your communications and data.

View details
93
Repo Health
8
Technical
68
Dependency
Built with
TypeScript 96%
Updated 1 weeks ago

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