Back to directory

TS-Pattern

A TypeScript pattern-matching library that lets code express complex conditional logic as a single expression, with type inference and compile-time exhaustiveness checking.

TS-Pattern

TS-Pattern brings pattern matching to TypeScript, letting a program match on nested objects, arrays, tuples, sets, maps and primitive values instead of chains of if/else or switch statements. It is fully typed, with type inference that narrows values as they are matched.

The library supports an exhaustiveness check through a dedicated method that verifies every possible case has been handled, wildcards for catching any or a specific type of value, predicates, unions, intersections and exclusion patterns, and a select function for pulling out matched properties. The same pattern syntax can also validate the shape of unknown data.

It has no runtime dependencies and adds only a small amount to a bundle's size. It is installed with npm, pnpm, yarn, bun or jsr and works in any JavaScript or TypeScript project.

More in Libraries

See all 292
  • OpenDecision

    deepanwadhwa.github.io

    An open-source Python package that answers typed questions about application state or documents using a local natural-language-inference model, returning structured choice, score, yes/no or evidence-relation values instead of free text.

  • TradeCanvas

    bonguynvan.github.io

    A zero-dependency canvas charting engine for building TradingView-style trading charts in JavaScript, with 66 technical indicators, 24 drawing tools, 17 chart types, and real-time streaming.

  • 3d retro graphics

    3d-retro.com

    A free, CC0-licensed library of copy-paste retro 3D and demoscene graphics effects, such as plasma, raytracing, and voxel terrain, runnable as WebGL 1 or plain 2D canvas.