Last active
February 3, 2021 13:49
-
-
Save DevWouter/c25fc313d12cfb4af28702edb6245566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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