TypeScript Pattern Matching Library
This library, `typescript-pattern-matching`, offers functional-style pattern matching capabilities for TypeScript, addressing the absence of native `match` or `case` keywords found in languages like F# or Haskell. It enables developers to implement sophisticated conditional logic by comparing data against various patterns, including value, record, wildcard, and negated patterns, with robust type inference. Version 1.0.1 is the latest release, but the project has seen no updates in over six years (last published December 2019). While it provides a basic implementation for structured pattern matching, its development has ceased, leading to a state of abandonment. More actively maintained and feature-rich alternatives have emerged in the TypeScript ecosystem. It requires TypeScript 3.7.3 or higher to function correctly.
Common errors
-
Property 'run' does not exist on type 'PatternMatcher<...>'
cause The `.run()` method, which executes the pattern matching, was omitted from the method chain.fixEnsure that every pattern matching chain explicitly ends with `.run()` or `.otherwise()`. -
Argument of type '...' is not assignable to parameter of type '...'
cause Type inference issues or a pattern not precisely matching the expected input type, often when `any` is not explicitly used or when patterns are too broad/narrow.fixReview the type definitions of your input and patterns. Explicitly cast types if necessary, or simplify patterns to ensure clearer type inference. For complex types, consider using type predicates in custom `when` clauses.
Warnings
- breaking This library has not been updated since December 2019, making it potentially incompatible with newer TypeScript versions or modern module resolutions (ESM, NodeNext).
- gotcha The library explicitly requires TypeScript 3.7.3 or higher. Using older TypeScript versions will result in compilation errors or unexpected behavior due to advanced type features utilized.
- gotcha Unlike some modern pattern matching libraries, `typescript-pattern-matching` does not offer compile-time exhaustiveness checking for all possible cases. This means it's possible to miss a case without a TypeScript error, potentially leading to runtime issues if `.otherwise()` is not used.
Install
-
npm install typescript-pattern-matching -
yarn add typescript-pattern-matching -
pnpm add typescript-pattern-matching
Imports
- match
const match = require('typescript-pattern-matching')import { match } from 'typescript-pattern-matching' - match().with()
match(value, pattern, handler)
match(value).with(pattern, handler).run()
Quickstart
import { match } from 'typescript-pattern-matching';
type Option<T> = { kind: 'none' } | { kind: 'some', value: T };
/**
* Processes an Option type using pattern matching to extract its value
* or handle the 'none' case.
*/
function processOption<T>(opt: Option<T>): T | string {
const result = match(opt)
.with({ kind: 'some' }, o => `Value is: ${o.value}`)
.with({ kind: 'none' }, () => 'No value present.')
.run();
return result as T | string; // Type assertion needed for example simplicity
}
const someValue: Option<string> = { kind: 'some', value: 'hello world' };
const noneValue: Option<number> = { kind: 'none' };
console.log(processOption(someValue));
console.log(processOption(noneValue));
// Example demonstrating .otherwise() for a default case
const unknownValue = { type: 'unknown' };
const processedUnknown = match(unknownValue)
.with({ type: 'known' }, v => `Known type: ${v.type}`)
.otherwise(() => 'Unknown type encountered.')
.run();
console.log(processedUnknown);