Skip to main content
InterviewHubPRO
Section Overview
Q45

Question 45 of 50

Part 3: Enterprise Architecture, Compiler Internals & Performance (Senior / Staff)

90%
TypeScript Master (50 Qs)•Part 3: Enterprise Architecture, Compiler Internals & Performance (Senior / Staff)

How do you implement a Type-Safe Router or Path Parameter Parser?

senior LevelCode ExampleRoutingFormsPerformance

In Simple Words: Using recursive template literal types, we can infer dynamic URL parameters from a route string:

typescript
type ExtractParams<Path extends string> = 
  Path extends `${string}:${infer Param}/${infer Rest}`
    ? Param | ExtractParams<`/${Rest}`>
    : Path extends `${string}:${infer Param}`
    ? Param
    : never;

type Route = "/users/:userId/posts/:postId";
type Params = ExtractParams<Route>; // "userId" | "postId"