{"id":15881,"library":"typescript-pattern-matching","title":"TypeScript Pattern Matching Library","description":"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.","status":"abandoned","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/WimJongeneel/ts-pattern-matching","tags":["javascript","TypeScript","Pattern Matching","Advanced Types","typescript"],"install":[{"cmd":"npm install typescript-pattern-matching","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-pattern-matching","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-pattern-matching","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const match = require('typescript-pattern-matching')","symbol":"match","correct":"import { match } from 'typescript-pattern-matching'"},{"note":"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()`.","wrong":"match(value, pattern, handler)","symbol":"match().with()","correct":"match(value).with(pattern, handler).run()"}],"quickstart":{"code":"import { match } from 'typescript-pattern-matching';\n\ntype Option<T> = { kind: 'none' } | { kind: 'some', value: T };\n\n/**\n * Processes an Option type using pattern matching to extract its value\n * or handle the 'none' case.\n */\nfunction processOption<T>(opt: Option<T>): T | string {\n  const result = match(opt)\n    .with({ kind: 'some' }, o => `Value is: ${o.value}`)\n    .with({ kind: 'none' }, () => 'No value present.')\n    .run();\n  return result as T | string; // Type assertion needed for example simplicity\n}\n\nconst someValue: Option<string> = { kind: 'some', value: 'hello world' };\nconst noneValue: Option<number> = { kind: 'none' };\n\nconsole.log(processOption(someValue));\nconsole.log(processOption(noneValue));\n\n// Example demonstrating .otherwise() for a default case\nconst unknownValue = { type: 'unknown' };\nconst processedUnknown = match(unknownValue)\n  .with({ type: 'known' }, v => `Known type: ${v.type}`)\n  .otherwise(() => 'Unknown type encountered.')\n  .run();\n\nconsole.log(processedUnknown);\n","lang":"typescript","description":"This quickstart demonstrates basic pattern matching on a discriminated union (`Option<T>`) and using `.otherwise()` for a default case."},"warnings":[{"fix":"Consider migrating to more actively maintained pattern matching libraries like `ts-pattern` for better compatibility, features, and continued support.","message":"This library has not been updated since December 2019, making it potentially incompatible with newer TypeScript versions or modern module resolutions (ESM, NodeNext).","severity":"breaking","affected_versions":">=1.0.1"},{"fix":"Ensure your project uses TypeScript version 3.7.3 or newer in `tsconfig.json`.","message":"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.","severity":"gotcha","affected_versions":"<3.7.3"},{"fix":"Always include an `.otherwise()` clause or thoroughly review your patterns to ensure all possible input shapes are handled.","message":"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.","severity":"gotcha","affected_versions":">=1.0.1"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure that every pattern matching chain explicitly ends with `.run()` or `.otherwise()`.","cause":"The `.run()` method, which executes the pattern matching, was omitted from the method chain.","error":"Property 'run' does not exist on type 'PatternMatcher<...>'"},{"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.","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.","error":"Argument of type '...' is not assignable to parameter of type '...'"}],"ecosystem":"npm"}