explaingit

gvergnaud/ts-pattern

Analysis updated 2026-06-24 · repo last pushed 2026-05-09

14,990TypeScriptAudience · developerComplexity · 2/5MaintainedSetup · easy

TLDR

TypeScript pattern matching library with exhaustiveness checks, so a missing case in a discriminated union becomes a compile error.

Mindmap

mindmap
  root((ts-pattern))
    Inputs
      Any TS value
      Pattern builders
    Outputs
      Matched handler result
      Type errors on missing cases
    Use Cases
      Match Redux actions
      Parse API responses
      Replace switch statements
    Tech Stack
      TypeScript
      Node
      npm
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Replace a long switch over a discriminated union with an exhaustive match

USE CASE 2

Handle untyped API responses by matching on their shape, not just equality

USE CASE 3

Write Redux or Zustand reducers that fail at compile time on a new action

USE CASE 4

Pattern-match React component states like loading, error, and success

What is it built with?

TypeScriptNodenpm

How does it compare?

gvergnaud/ts-patternscalar/scalarcaprover/caprover
Stars14,99015,00915,019
LanguageTypeScriptTypeScriptTypeScript
Last pushed2026-05-092026-05-22
MaintenanceMaintainedMaintained
Setup difficultyeasyeasymoderate
Complexity2/53/53/5
Audiencedeveloperdeveloperops devops

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min

Single npm install with no runtime config, the only real requirement is TypeScript strict mode for the exhaustiveness check to work.

In plain English

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.

Copy-paste prompts

Prompt 1
Convert this switch statement to ts-pattern with .exhaustive so I cannot forget a case
Prompt 2
Use ts-pattern to handle a Result type with ok and err variants in my fetch wrapper
Prompt 3
Write a React reducer with ts-pattern that handles login, logout, and refresh actions
Prompt 4
Show me how to use P.select to pull nested fields out of a matched object
Prompt 5
Match the JSON shape of an unknown webhook payload with ts-pattern and narrow its type

Frequently asked questions

What is ts-pattern?

TypeScript pattern matching library with exhaustiveness checks, so a missing case in a discriminated union becomes a compile error.

What language is ts-pattern written in?

Mainly TypeScript. The stack also includes TypeScript, Node, npm.

Is ts-pattern actively maintained?

Maintained — commit in last 6 months (last push 2026-05-09).

How hard is ts-pattern to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is ts-pattern for?

Mainly developer.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.