Dev Helpers

TypeScript / Literal unions

Derive a union from const data

Dev Helpers · November 20, 2025

A readonly literal list can be the source of truth for both runtime choices and a compile-time union. It avoids maintaining a duplicate list of allowed names.

Worked example

const statuses = ["queued", "running", "done"] as const;
type Status = (typeof statuses)[number];
console.log(((s: Status) => s)("queued"));

Result

queued

Implementation notes

A value imported from JSON usually has broader types than a literal declared with const assertions. Keep the source of the union under your control.

Try it yourself

Add a new member and check which dependent types expand automatically.

Open the TypeScript example →

Editorial date November 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.