Skip to content

Instantly share code, notes, and snippets.

@DevWouter
Last active February 3, 2021 13:49
Show Gist options
  • Save DevWouter/c25fc313d12cfb4af28702edb6245566 to your computer and use it in GitHub Desktop.
Save DevWouter/c25fc313d12cfb4af28702edb6245566 to your computer and use it in GitHub Desktop.
type RouteData = PathArgs<"/company/:companyId/customer/:customerId/vendor-user/:vendorUserId">;
// .
// /|\
var routeData: RouteData = { // |
companyId: "",// <-- Strong typed based on URL |
customerId: "",// <-- Strong typed based on URL |
vendorUserId: "", // <-- Strong typed based on URL |
};
// The magic
type PathParams<Path extends string> =
Path extends `:${infer Param}/${infer Rest}` ? Param | PathParams<Rest> :
Path extends `:${infer Param}` ? Param :
Path extends `${infer _Prefix}:${infer Rest}` ? PathParams<`:${Rest}`> :
never;
type PathArgs<Path extends string> = {
[K in PathParams<Path>]: string
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment