Dev Helpers

TypeScript / Discriminated unions

Extract all discriminant names

Dev Helpers · September 20, 2025

Discriminant fields tie runtime branches to a finite set of names. Extracting their union keeps routing or rendering logic aligned with actual variants.

Worked example

type Result =
  { kind: "ok"; value: number } | { kind: "error"; message: string };
const results: Result[] = [
  { kind: "ok", value: 3 },
  { kind: "error", message: "offline" },
];
console.log(((kind: Result["kind"]) => kind)("ok"));

Result

ok

Implementation notes

A duplicate discriminant across variants can hide an ambiguous state model. A union of names alone will not reveal that duplication.

Try it yourself

Introduce another variant and inspect the resulting set of names.

Open the TypeScript example →

Editorial date September 20, 2025 · Published December 6, 2026

Related articles

Reject fractional cent amounts

A numeric type cannot by itself guarantee whole cents. Validate integrality where money enters the typed part of the application.

Infer route names from a registry

A route registry can drive both lookup and allowed route names. Type the keys from the registry so a removed route cannot survive in a separate union.