Define variants once
Create a discriminated union with constructors, inferred payloads, and narrowing built in.
dismatch
Type-safe pattern matching for TypeScript
`dismatch` gives you constructors, type guards, and exhaustive matching without forcing a framework or a compiler trick into your application architecture.
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,
});Create a discriminated union with constructors, inferred payloads, and narrowing built in.
Encode every branch at the callsite and let TypeScript fail fast when a case is missing.
Keep the surface area small enough for libraries, apps, and playground-style prototyping.
Why teams use it
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
Package manager
npm install dismatchStart exploring
Use the playground to test constructors, pattern matching, and exhaustiveness before you wire the library into your app code.
Launch playgroundAPI shape
Build tagged objects without hand-rolling repetitive factory functions and variant types.
Narrow branch inputs predictably when you need imperative control flow instead of a matcher.
Keep return types aligned with the handled cases and let missing branches fail at compile time.