Skip to content

Instantly share code, notes, and snippets.

@olafsulich
Created September 7, 2021 20:35
Show Gist options
  • Save olafsulich/b0dc694ee474ee8c9b303a873b5bc026 to your computer and use it in GitHub Desktop.
Save olafsulich/b0dc694ee474ee8c9b303a873b5bc026 to your computer and use it in GitHub Desktop.
TypeScript - parse route parameters
type ParseRouteParameters<Route> = Route extends `${string}/:${infer Param}/${infer Rest}`
? { [Entry in Param | keyof ParseRouteParameters<`/${Rest}`>]: string }
: Route extends `${string}/:${infer Param}`
? { [Entry in Param]: string }
: {};
type X = ParseRouteParameters<'/api/:what/:is/notyou/:happening'>;
// type X = {
// what: string,
// is: string,
// happening: string,
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment