Dev Helpers
← Back

Select routes matching a method

Select routes matching a method 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(
  ((method: Route["method"]) =>
    Object.values(routes).filter((r) => r.method === method))("GET"),
);

Sample output

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