Configs2026年5月15日·1 分钟阅读

ts-pattern — Exhaustive Pattern Matching for TypeScript

Type-safe pattern matching library that brings ML-style match expressions to TypeScript with full exhaustiveness checking.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
ts-pattern Overview
通用 CLI 安装命令
npx tokrepo install 6f594f9c-4ff5-11f1-9bc6-00163e2b0d79

Introduction

ts-pattern brings pattern matching to TypeScript, letting developers replace nested if-else chains and switch statements with declarative match expressions. The library checks at compile time that every possible case is handled, catching missing branches before runtime. It supports matching on objects, arrays, tuples, literals, and custom predicates.

What ts-pattern Does

  • Matches values against structural patterns with nested object and array support
  • Enforces exhaustiveness checking at the type level to prevent missed cases
  • Provides wildcard patterns, guard clauses, and union matching via P helpers
  • Narrows TypeScript types automatically inside each branch handler
  • Supports chaining with .when(), .with(), .otherwise(), and .exhaustive()

Architecture Overview

ts-pattern uses TypeScript's type inference to track which patterns have been handled. Each .with() call narrows the remaining type, and .exhaustive() ensures the residual type is never. The runtime matching engine walks the pattern tree, comparing shape and value at each level. Pattern helpers like P.string, P.number, and P.array create reusable matchers.

Self-Hosting & Configuration

  • Install via npm, yarn, or pnpm with no additional dependencies
  • Requires TypeScript 4.5 or later for full type inference
  • Works with any bundler; ships ESM and CommonJS formats
  • No runtime configuration needed beyond importing match and P
  • Integrates with existing TypeScript projects without build changes

Key Features

  • Compile-time exhaustiveness guarantees that eliminate runtime surprises
  • Deep nested pattern matching on objects, arrays, and tuples
  • Built-in pattern helpers for primitives, unions, and optional values
  • Automatic type narrowing inside each matched branch
  • Zero dependencies and small bundle footprint

Comparison with Similar Tools

  • switch/case — no exhaustiveness checking, verbose, and limited to primitives
  • fp-ts match — part of a larger FP ecosystem, more ceremony for simple cases
  • effect/Match — similar API within the Effect ecosystem, tighter coupling
  • purify-ts — ADT matching with Maybe/Either types, narrower focus

FAQ

Q: Does ts-pattern add runtime overhead? A: Minimal. The matching engine is lightweight and patterns compile to simple property checks.

Q: Can I use ts-pattern with JavaScript instead of TypeScript? A: The library works in JavaScript but you lose exhaustiveness checking and type narrowing, which are the main benefits.

Q: How does exhaustiveness checking work? A: After each .with() call, TypeScript narrows the input type. Calling .exhaustive() errors at compile time if the remaining type is not never.

Q: Does it support async handlers? A: Yes. Handler functions can return promises, and the overall match expression will resolve to the awaited type.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产