ts-pattern exhaustive matching on discriminated unions: working versions (tested 2026-10-15)
Match TypeScript discriminated unions exhaustively with ts-pattern match, P selectors and .exhaustive(), covering nested event handling without unsafe defaults.
passed2 passed · 0 failed2026-10-15 16:06 UTC- Status
- passed
- Last passed
- 2026-10-15 16:06 UTC
- Tests
- 2 / 2
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
ts-pattern@5.9.2@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38764699834
The code
Exactly the files that passed. Your agent can pull them with get_recipe("ts-pattern-discriminated-unions") (install), or see them on GitHub.
package.json
{
"name": "ts-pattern-discriminated-unions",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"ts-pattern": "5.9.2"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/events.ts
import { match, P } from "ts-pattern";
export type Event =
| { type: "user_created"; id: string; email: string }
| { type: "user_deleted"; id: string; reason: string }
| { type: "payment"; id: string; amount: number; currency: "USD" | "EUR" }
| { type: "refund"; id: string; amount: number; currency: "USD" | "EUR"; paymentId: string };
export function describeEvent(e: Event): string {
return match(e)
.with({ type: "user_created" }, (x) => `created ${x.email}`)
.with({ type: "user_deleted", reason: P.string.minLength(1) }, (x) => `deleted ${x.id}: ${x.reason}`)
.with({ type: "payment", currency: "USD", amount: P.number.gt(0) }, (x) => `charge $${x.amount}`)
.with({ type: "payment", currency: "EUR" }, (x) => `charge €${x.amount}`)
.with({ type: "refund" }, (x) => `refund ${x.amount} ${x.currency} for ${x.paymentId}`)
.exhaustive();
}
export function totalUsd(events: Event[]): number {
return events.reduce((sum, e) => sum + match(e)
.with({ type: "payment", currency: "USD" }, (x) => x.amount)
.with({ type: "refund", currency: "USD" }, (x) => -x.amount)
.otherwise(() => 0), 0);
}
test/events.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { describeEvent, totalUsd, type Event } from "../src/events.ts";
test("exhaustive descriptions", () => {
assert.equal(describeEvent({ type: "user_created", id: "1", email: "[email protected]" }), "created [email protected]");
assert.equal(describeEvent({ type: "payment", id: "p1", amount: 12, currency: "USD" }), "charge $12");
assert.equal(describeEvent({ type: "refund", id: "r1", amount: 5, currency: "USD", paymentId: "p1" }), "refund 5 USD for p1");
});
test("totals usd payments and refunds", () => {
const events: Event[] = [
{ type: "payment", id: "1", amount: 20, currency: "USD" },
{ type: "payment", id: "2", amount: 5, currency: "EUR" },
{ type: "refund", id: "3", amount: 3, currency: "USD", paymentId: "1" },
];
assert.equal(totalUsd(events), 17);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.