Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pattern-matching-ts

> 编程语言
Open source

⚡ Pattern Matching in Typescript

190 stars0 likes2 views
WebsiteGitHub

About

⚡ Pattern Matching in Typescript

Pattern matching in Typescript.

Pattern Matching is a declarative much more powerful and less verbose alternative to imperatives "if/else" conditions.

A definition can be found inside Scala Documentation

“Pattern matching tests whether a given value (or sequence of values) has the shape defined by a pattern, and, if it does, binds the variables in the pattern to the corresponding components of the value (or sequence of values).”

In Functional Programming languages, there're built-in keywords for Pattern Matching. Typescript though is one language that works very well with Functional Programming but lacks this feature.

This package aims to bring Pattern Matching feature to Typescript through Discriminated Union Types / Algebraic Data Types.

Index

  • Installation
  • Usage

  • Match
    • Match Option
    • Match Default
  • MatchW
    • MatchW Option
    • MatchW Either
    • MatchW Default

  • License

Installation

yarn

bash
yarn add pattern-matching-ts

npm

bash
npm install --save pattern-matching-ts

Usage

MatchW

Option MatchW

typescript
import * as M from 'pattern-matching-ts/lib/match'
import { pipe } from 'fp-ts/lib/function'
import * as O from 'fp-ts/lib/Option'

const optionMatching = (o: unknown) =>
  pipe(
    o,
    M.matchW('_tag')({
      Some: ({ value }) => 'Something: ' + value,
      None: () => 'Nothing',
      _: () => 'Default'
    })
  )

assert.deepStrictEqual(optionMatching(O.some('data')), 'Something: data')
assert.deepStrictEqual(optionMatching(O.none), 'Nothing')
assert.deepStrictEqual(optionMatching((undefined as unknown) as O.None), 'Default')

Either MatchW

typescript
import * as M from 'pattern-matching-ts/lib/match'
import { pipe } from 'fp-ts/lib/function'
import * as E from 'fp-ts/lib/Either'

type RGB = Record
const either = (maybeRgb: E.Either) =>
  pipe(
    maybeRgb,
    M.matchW('_tag')({
      Left: ({ left }) => 'Error: ' + left,
      Right: ({ right: { r, g, b } }) => `Red: ${r} | Green: ${g} | Blue: ${b}`
    })
  )

assert.deepStrictEqual(either(E.right({ r: 255, g: 255, b: 0 })), 'Red: 255 | Green: 255 | Blue: 0')

Default MatchW

…

Match

Option Match

typescript
import * as M from 'pattern-matching-ts/lib/match'
import * as O from 'fp-ts/lib/Option'

const optionMatching = M.match, string>({
  Some: (x) => `Something: ${x.value}`,
  None: () => 'Nothing'
})

assert.deepStrictEqual(optionMatching(O.some('data')), 'Something: data')
assert.deepStrictEqual(optionMatching(O.none), 'Nothing')

Default Match

…

Blog posts that introduces the API.

Pattern Matching in Typescript

Pipeable Pattern Matching in Typescript

MIT

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptfpfp-tsfunctional-programmingmatching

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言