Dev Helpers
← Back

Build a route with a required parameter

Build a route with a required parameter 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(
  ((id: number): string => "/jobs/" + encodeURIComponent(String(id)))(7),
);

Sample output

/jobs/7