Analysis updated 2026-06-24 · repo last pushed 2026-05-09
Replace a long switch over a discriminated union with an exhaustive match
Handle untyped API responses by matching on their shape, not just equality
Write Redux or Zustand reducers that fail at compile time on a new action
Pattern-match React component states like loading, error, and success
| gvergnaud/ts-pattern | scalar/scalar | caprover/caprover | |
|---|---|---|---|
| Stars | 14,990 | 15,009 | 15,019 |
| Language | TypeScript | TypeScript | TypeScript |
| Last pushed | 2026-05-09 | 2026-05-22 | — |
| Maintenance | Maintained | Maintained | — |
| Setup difficulty | easy | easy | moderate |
| Complexity | 2/5 | 3/5 | 3/5 |
| Audience | developer | developer | ops devops |
Figures from each repo's GitHub metadata at analysis time.
Single npm install with no runtime config, the only real requirement is TypeScript strict mode for the exhaustiveness check to work.
TS-Pattern is a TypeScript library that adds pattern matching to JavaScript and TypeScript code. Pattern matching is a way of writing branching logic that asks not just whether a value equals something, but whether a value has a certain shape. The README explains that pattern matching is built into languages such as Python, Rust, Swift, Elixir, and Haskell, and there is a TC39 proposal to add it to JavaScript itself, but until that lands TS-Pattern provides the same idea as a small library. The main entry point is a function called match. You call match on a value, chain one or more .with calls that describe possible shapes of that value along with a handler function, and end the chain with either .exhaustive or .otherwise. The .exhaustive call uses the TypeScript type system to check that every possible case in a union type has been covered. If a case is missing, the code will not compile. This catches the common bug where someone adds a new variant to a type and forgets to update the switch statement that handles it. The library also exports a P object that contains pattern builders. Examples in the README include P.string and P.number as wildcards for those types, P.union and P.intersection for combining patterns, P.not for excluding shapes, P.when for custom predicates, and P.select for capturing parts of the matched value and passing them to the handler. Patterns can be nested inside object and array shapes, so you can ask whether a result is an ok wrapper containing data of type img with any src value, for example. The README pitches the library as small, around 2 kilobytes when bundled, and lists installation instructions for npm, pnpm, yarn, bun, and JSR. There are sandbox demos on StackBlitz showing the library used in a state reducer, in a React app that fetches GIFs, and in handlers for untyped API responses. The author also links to an online TypeScript course called Type-Level TypeScript. The full README is longer than what was shown.
TypeScript pattern matching library with exhaustiveness checks, so a missing case in a discriminated union becomes a compile error.
Mainly TypeScript. The stack also includes TypeScript, Node, npm.
Maintained — commit in last 6 months (last push 2026-05-09).
Setup difficulty is rated easy, with roughly 5min to a first successful run.
Mainly developer.
This repo across BitVibe Labs
Verify against the repo before relying on details.