ts-pattern is a pattern matching library for TypeScript that enables exhaustive, type-safe branching logic with smart type inference.
The library solves the problem of writing complex conditional logic by providing a functional programming approach to branching. Instead of nested if/else or switch statements, developers write pattern expressions that match against data structures. The tool performs exhaustiveness checking to ensure all possible cases are handled, catching logic errors at compile time. It supports matching on nested objects, arrays, tuples, sets, maps, and primitive types, with an expressive API that includes wildcards, predicates, unions, intersections, and exclusion patterns.
Developers should choose this tool when working with complex data structures where traditional conditionals become unwieldy or error-prone. It suits projects where type safety and code clarity are priorities, particularly those handling discriminated unions, nested data validation, or state machines. The library is lightweight, adding only a small bundle footprint to projects.
The project maintains active engagement with its community through regular issue responses and pull request reviews. Development follows a structured approach with clear documentation and examples. The maintainers prioritize type safety and inference quality, regularly refining how the library infers types from patterns. The codebase receives consistent updates addressing edge cases and expanding pattern matching capabilities.