Dev Helpers
← Back

Match route templates by segments

Match route templates by segments 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(
  ((parts: string[]): parts is [string, string] => parts.length === 2)(
    "jobs/7".split("/"),
  ),
);

Sample output

true