07 · Senior Frontend Engineering
hardcodingtypescript~18 min

Type-Safe Route Params

Objective: Use template literal types and recursion to make an API contract checkable at compile time.

Design `RouteParams<Path>` so `RouteParams<"/users/:id/posts/:postId">` is `{ id: string; postId: string }`. A route with no params is `{}`.

  • Extract each `:name` segment as a string key.
  • Handle any number of params.
  • Static segments contribute nothing.
  • The result must be a single flat object type.
TypeScript (strict)

⌘/Ctrl + Enter to type-check · Esc then Tab leaves the editor

Output
> Nothing has run yet.
Interview follow-up questions
  • Where would you use this: router, API client, link builder?
  • What are the compile-time cost risks of deep template-literal recursion?
  • How would you add optional params like :id?