Dev Helpers
← Back

Create a route-specific body contract

Create a route-specific body contract 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(
  (() => {
    type Bodies = { create: { name: string }; list: undefined };
    const body: Bodies["create"] = { name: "build" };
    return body;
  })(),
);

Sample output

{ name: 'build' }