Skip to content

ts-pattern

A TypeScript pattern matching library that checks you've covered every case.

Stars
15.2k
Forks
181
Licence
MIT
Language
TypeScript
Last update
23 days ago
Type
Library

What it is

TS-Pattern is a TypeScript library that lets you branch on the shape of data using a compact match expression instead of long if/else or switch chains. Its .exhaustive() check lets the compiler flag cases you have not handled. It is installed from npm and is described in the README as only about 2kB.

  • Write a state reducer that branches on both current state and incoming event
  • Match nested object shapes and extract a value with P.select()
  • Use P.when() to add a predicate condition to a pattern
  • Check whether a value has a given shape with isMatching

Great for

  • TypeScript developers who write many if/else or switch branches over tagged unions
  • Frontend developers handling state and event reducers
  • Teams that want the type checker to catch missing cases
  • Developers handling untyped API responses who want to validate their shape

Not ideal for

  • Plain JavaScript projects, since the library is built around TypeScript type inference
  • People who only need a simple single if statement, where the library adds little
  • Teams that cannot add a runtime dependency to their bundle
Step-by-step guide: how to use ts-patternInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Node.js or a compatible package manager such as npm, pnpm, yarn, or bun

    Install via npm

    npm install ts-pattern

    Install via pnpm

    pnpm add ts-pattern
  2. 2

    Run it

    • Install the package with npm install ts-pattern
    • Import match and P from 'ts-pattern'
    • Call match(value) and chain .with(pattern, handler) calls
    • End the chain with .exhaustive() or .otherwise()
  3. 3

    Get the most out of it

    • Use P.select() to pull values out of a matched object
    • Use P.when() to add custom predicate conditions
    • Use P.union() and P.not() for more flexible patterns
    • Use .returnType<T>() to set the return type explicitly
    • Use isMatching to validate data shape without branching

Downloads

Strengths

  • Exhaustiveness checking via .exhaustive() catches unhandled cases at compile time
  • Supports nested objects, arrays, sets, maps, and primitives in one API
  • Small bundle size, stated as about 2kB in the README

Trade-offs

  • Its value depends on TypeScript; the README does not describe a plain JavaScript workflow
  • The README is truncated, so details of advanced type behavior are not verified here
  • Pattern matching is not built into JavaScript yet, so the code relies on a userland library

How it compares

ProjectStarsTypeLevelUpdatedBest for
ts-pattern (this)15.2kLibrarybeginner23 days agoTypeScript developers who write many if/else or switch branches over tagged unions
zod44.2kLibrarybeginner6 days agoTypeScript developers who want runtime checks that match their static types
whisper.cpp54.9kLibraryintermediate7 days agoDevelopers who want to embed offline speech-to-text in a C, C++, or other native app
CopilotKit38.5kLibraryintermediate4 days agoDevelopers adding an AI assistant or agent interface to a React or Next.js app
nano-vllm16.3kLibraryintermediate6 months agoDevelopers who want to understand how LLM inference engines work internally
faster-whisper26.4kLibraryintermediate5 days agoPython developers who need to add speech-to-text to an application

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.