ts-pattern

Exhaustive pattern matching for TypeScript. ts-pattern provides a match() function that handles all cases of discriminated unions, union types, and complex data structures with compile-time exhaustiveness checking. The TypeScript compiler guarantees all cases are handled — missing a case is a type error. Replaces verbose switch statements with readable, type-safe pattern matching like Rust's match or Haskell's case expressions.

Evaluated Mar 06, 2026 (0d ago) v5.x
Homepage ↗ Repo ↗ Developer Tools typescript pattern-matching switch exhaustive type-safe functional discriminated-unions
⚙ Agent Friendliness
70
/ 100
Can an agent use this?
🔒 Security
99
/ 100
Is it safe for agents?
⚡ Reliability
88
/ 100
Does it work consistently?

Score Breakdown

⚙ Agent Friendliness

MCP Quality
--
Documentation
90
Error Messages
88
Auth Simplicity
100
Rate Limits
100

🔒 Security

TLS Enforcement
100
Auth Strength
100
Scope Granularity
100
Dep. Hygiene
95
Secret Handling
100

Local library — no network access. Zero dependencies — minimal supply chain risk. MIT licensed.

⚡ Reliability

Uptime/SLA
100
Version Stability
82
Breaking Changes
80
Error Recovery
90
AF Security Reliability

Best When

You have discriminated union types in TypeScript and want compile-time guarantees that all cases are handled — especially for complex state management, command handling, or API response processing.

Avoid When

Simple boolean conditions, non-TypeScript code, or use cases where basic switch handles the complexity adequately.

Use Cases

  • Handle all cases of discriminated union types (Redux actions, API response states, event types) with TypeScript compile-time completeness guarantees
  • Replace verbose switch/if-else chains for tagged union handling with readable match() patterns that TypeScript verifies are exhaustive
  • Handle agent tool responses typed as discriminated unions ensuring all success, error, and pending states are explicitly addressed
  • Pattern match on complex nested data structures using ts-pattern's structural matching patterns without custom type guards
  • Implement state machine transitions using match() to exhaustively handle all current states and produce correct next states

Not For

  • Simple single-condition checks — ts-pattern is overkill for basic if/else
  • Teams not using TypeScript — ts-pattern's value is 100% in TypeScript's type system
  • Performance-critical hot paths — match() adds minor function call overhead compared to switch

Interface

REST API
No
GraphQL
No
gRPC
No
MCP Server
No
SDK
Yes
Webhooks
No

Authentication

Methods: none
OAuth: No Scopes: No

Local library — no authentication.

Pricing

Model: open_source
Free tier: Yes
Requires CC: No

ts-pattern is MIT open source. Free for personal and commercial use.

Agent Metadata

Pagination
none
Idempotent
Full
Retry Guidance
Not documented

Known Gotchas

  • Exhaustive checking requires calling .exhaustive() — using .otherwise(defaultFn) disables exhaustiveness checking and allows unhandled cases
  • ts-pattern 5.x changed the API from 4.x — P.select(), P.when(), P.array() patterns have different syntax in v5; check migration guide when upgrading
  • Pattern matching on class instances requires P.instanceOf(ClassName) — structural matching doesn't work for class instances with the same shape
  • Deep patterns are validated at compile time but matched at runtime — a very deep pattern on a large object has minimal runtime cost but may require careful TypeScript inference for complex union types
  • TypeScript inference for very complex discriminated unions (10+ members) can slow TypeScript compilation when used with ts-pattern — if tsc becomes slow, simplify union types
  • P.select() for extracting matched values requires TypeScript 4.1+ for named selections to work with typed extraction — verify TypeScript version compatibility

Alternatives

Full Evaluation Report

Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for ts-pattern.

$99

Scores are editorial opinions as of 2026-03-06.

5208
Packages Evaluated
26151
Need Evaluation
173
Need Re-evaluation
Community Powered