An exhaustive pattern matching library for TypeScript with smart type inference and expressive API.
TS-Pattern is a TypeScript library that implements exhaustive pattern matching with smart type inference. It allows developers to replace complex conditional logic (like nested if/else or switch statements) with a single, expressive pattern-matching expression that ensures all possible cases are handled at compile time.
TypeScript developers building applications with complex state management, data validation, or conditional branching who want safer and more maintainable code through exhaustive checking.
Developers choose TS-Pattern for its robust type safety, exhaustive checking that prevents missed cases, and expressive API that reduces boilerplate while making conditional logic more readable and declarative.
🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
The .exhaustive() method enforces handling all possible union type cases at compilation, catching missed cases early as shown in state reducer examples, reducing bugs in evolving codebases.
Patterns automatically narrow types in handlers, like inferring specific object shapes from matches, enhancing type safety without manual annotations, evident in examples with P.select().
Supports complex patterns for objects, arrays, tuples, and more with wildcards, guards, and selections, allowing concise code for nested data structures, as demonstrated in the API reference.
At only ~2kB, the library has negligible impact on bundle size, making it production-suitable, highlighted in the README's features section.
Exhaustive checking requires more work from TypeScript's type checker, potentially slowing build times, which the README admits as a trade-off under the .exhaustive() section.
Mastering all pattern types like P.union, P.intersection, and advanced selections can be challenging, especially for developers unfamiliar with functional programming concepts, despite comprehensive documentation.
Cannot leverage its core benefits in plain JavaScript or mixed codebases, as it relies on TypeScript's type system for exhaustiveness and inference, restricting broader adoption.