d/

dismatch

Type-safe pattern matching for TypeScript

Production-ready API surface, live browser playground

Type-safe discriminated unions for teams that want less ceremony.

`dismatch` gives you constructors, type guards, and exhaustive matching without forcing a framework or a compiler trick into your application architecture.

Installnpm i dismatch
import { createUnion, match } from "dismatch";

const Result = createUnion("status", {
  ok: (data: string) => ({ data }),
  error: (message: string) => ({ message }),
});

const output = match(Result.ok("ready"), {
  ok: ({ data }) => data.toUpperCase(),
  error: ({ message }) => message,
});

Define variants once

Create a discriminated union with constructors, inferred payloads, and narrowing built in.

Match exhaustively

Encode every branch at the callsite and let TypeScript fail fast when a case is missing.

Stay runtime-light

Keep the surface area small enough for libraries, apps, and playground-style prototyping.

Why teams use it

Safer branching, cleaner callsites, fewer repeated tags.

The library is small enough to stay out of your way, but opinionated enough to make impossible states harder to represent and easier to eliminate.

Construct tagged variants without repeating string literals.

Generate type guards that narrow cleanly in userland code.

Write exhaustive matchers with readable result branches.

Install

Ship it as a dependency, explore it in the browser.

Package manager

npm install dismatch

Start exploring

Use the playground to test constructors, pattern matching, and exhaustiveness before you wire the library into your app code.

Launch playground

API shape

Small surface area, strong defaults.

Constructors

Build tagged objects without hand-rolling repetitive factory functions and variant types.

Type guards

Narrow branch inputs predictably when you need imperative control flow instead of a matcher.

Exhaustive match

Keep return types aligned with the handled cases and let missing branches fail at compile time.