Dev Helpers

TypeScript / Typed routes

Infer route names from a registry

Dev Helpers · November 11, 2026

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.

Worked example

type Route = { path: string; method: "GET" | "POST" };
const routes = {
  list: { path: "/jobs", method: "GET" },
  create: { path: "/jobs", method: "POST" },
} as const satisfies Record<string, Route>;
console.log(((key: keyof typeof routes) => routes[key])("list"));

Result

{ path: '/jobs', method: 'GET' }

Implementation notes

A lookup accepts a string at runtime even when the public helper uses a restricted key type. Validate external route names before indexing.

Try it yourself

Rename a registry entry and check a caller using its previous name.

Open the TypeScript example →

Editorial date November 11, 2026 · 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 a tuple element union

Indexing a tuple's type by number yields a union of its element types. Literal inference determines how precise that union becomes.