Dev Helpers
← Back

Decode an optional route segment

Decode an optional route segment 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: readonly string[]) => parts.at(1) ?? "index")(["jobs"]));

Sample output

index