ts-pattern
Backend / API library · Free
TypeScript pattern matching with exhaustive checking and type inference: it expresses branching over complex data structures through clear cases.
Also known as: gvergnaud/ts-pattern; pattern-matching-library

What you can use for free
Code and local use under MIT.
Paid features and costs
Computing resources, self-hosting, and external services are paid for separately if used.
Access and limitations
Code and local use under MIT. Computing resources, self-hosting, and external services are paid for separately if used.
Use cases
- Express exhaustive branching over typed data
- Connect pattern branches to handling every data variant
Example workflow
A possible workflow; this is not a report of hands-on testing.
Express exhaustive branching over typed data.
Before you start
- A data contract or HTTP workflow.
- A project with a supported runtime.
Steps
- Define the state union and expected matching cases.
- Connect pattern branches to handling every data variant.
- Add error handling and an integration check of the result.
Check your result
- Invalid and missing data are handled predictably.
- Types and the actual response agree; errors are not hidden.
Choosing ts-pattern
Express exhaustive branching over typed data
Choose this when
- Express exhaustive branching over typed data
- Connect pattern branches to handling every data variant
Requirements
- The README shows npm install ts-pattern; use named P.select for multiple extracted fields, since a pattern can contain only one anonymous selection.
Things to consider
- Costs and limits of free use: Computing resources, self-hosting, and external services are paid for separately if used.
- Terms of use: MIT for the code; dependencies have separate licenses.
- Completeness checking adds compiler work. With incorrect types, an unexpected value can still reach .exhaustive() and trigger a runtime error; a custom handler can be provided.
Frequently asked questions
What can you use for free in ts-pattern?
Code and local use under MIT.
What additional costs might arise with ts-pattern?
Computing resources, self-hosting, and external services are paid for separately if used.
What terms of use matter for ts-pattern?
MIT for the code; dependencies have separate licenses.
How do you get started with ts-pattern?
Define the state union and expected matching cases. Connect pattern branches to handling every data variant. Add error handling and an integration check of the result.
How does TS-Pattern help process nested data?
match checks .with() branches against value structure; P.select extracts selected nested parts into a handler, and P.when adds a conditional check. .exhaustive() enables compile-time branching completeness checks.
License
MIT for the code; dependencies have separate licenses.
MIT — permissive license
You can use, modify, distribute, and sell the code, including in a closed-source commercial product. When sharing copies or substantial portions of the code, retain the license text and copyright notice. You do not need to open-source your entire project; the author provides no warranties.
License termsTechnologies and topics
- ts-pattern
- API
- Development