You can extract types from path with TypeScript template literal types
like:
typeExtractRouteParams<T>=stringextendsT?Record<string,string>:Textends`${infer_Start}:${inferParam}/${inferRest}`?{[kinParam|keyofExtractRouteParams<Rest>]:string}:Textends`${infer_Start}:${inferParam}`?{[kinParam]:string}:{};typeParams=ExtractRouteParams<'/post/:id'>;// // type is {id: string}typeParams=ExtractRouteParams<'/calendar/:year/:month'>;// type is {year: string; month: string}
You can extract types from path with TypeScript template literal types
like:
Can read more about this here and here
Thank you for sharing this feature! It is shocking how this is possible in TS. I am so excited to update my example!
The post & examples are updated, and many duplicates are removed. Your suggestion helped a lot.