Recursive Template Literal Type Parsing
This snippet uses template literal types and recursive conditional types to extract dynamic path parameters from a string literal at compile time. It demonstrates how TypeScript can parse strings to build complex object types based on naming conventions like ':param'.
type ExtractParams<T extends string> =T extends `${string}:${infer Param}/${infer Rest}`? { [K in Param | keyof ExtractParams<Rest>]: string }: T extends `${string}:${infer Param}`? { [K in Param]: string }: {};const route = "/user/:id/posts/:postId";type RouteParams = ExtractParams<typeof route>;// Result: { id: string; postId: string; }