Dev Helpers
← Back

Infer route names from a registry

Infer route names from a registry using TypeScript.

Open this example in the interactive library

Source code

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"));

Sample output

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