Registry / serialization / typescript-pattern-matching

typescript-pattern-matching

JSON →
library1.0.1jsnpmunverified

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.

npm install typescript-pattern-matching
INSTALL
IMPORT
SIG · TYPESCRIPT-PATTERN
T
typescript-pattern-matching
serializationjavascriptv1.0.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

match
✓ import { match } from 'typescript-pattern-matching'
✗ const match = require('typescript-pattern-matching')
The library primarily uses ES Module imports. CommonJS `require` syntax is generally not recommended in modern TypeScript projects and may not be fully supported for type inference.
match().with()
✓ match(value).with(pattern, handler).run()
✗ match(value, pattern, handler)
The pattern matching API is fluent, requiring method chaining starting with `match(value)`, followed by one or more `.with(pattern, handler)` clauses, and terminated by `.run()` or `.otherwise()`.

This quickstart demonstrates basic pattern matching on a discriminated union (`Option<T>`) and using `.otherwise()` for a default case.

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);
Debug
Known issues
breakingThis library has not been updated since December 2019, making it potentially incompatible with newer TypeScript versions or modern module resolutions (ESM, NodeNext).
fix
Consider migrating to more actively maintained pattern matching libraries like `ts-pattern` for better compatibility, features, and continued support.
affects: >=1.0.1
gotchaThe 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.
fix
Ensure your project uses TypeScript version 3.7.3 or newer in `tsconfig.json`.
affects: <3.7.3
gotchaUnlike 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.
fix
Always include an `.otherwise()` clause or thoroughly review your patterns to ensure all possible input shapes are handled.
affects: >=1.0.1
Errors
Common errors & fixes
Property 'run' does not exist on type 'PatternMatcher<...>'
The `.run()` method, which executes the pattern matching, was omitted from the method chain.
fix
Ensure that every pattern matching chain explicitly ends with `.run()` or `.otherwise()`.
Argument of type '...' is not assignable to parameter of type '...'
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.
fix
Review 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.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
OpenAI (training)
2
Resources
typescript-pattern-matching — npm install typescript-pattern-matching · libregistry