Last active
September 20, 2023 19:28
-
-
Save pmn4/1d76986184d7b11f90ca7a88747f5340 to your computer and use it in GitHub Desktop.
a TypeScript type for parsing URL path parameters
This file contains hidden or 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
| /* | |
| I find that the edges of serialization are the greatest weaknesses for | |
| TypeScript projects. Further, with API routes, I like to be as declarative | |
| as possible, so the code (the types) can be documentation for both client | |
| and server. | |
| With this type, URL path parameters can be extracted from path string: | |
| */ | |
| // URL param values come in as a string or an array of strings | |
| type AsQuery<T extends string> = { [K in T]: string | string[] }; | |
| // split out Path into a prefix, param, and suffix, where param is wrapped in | |
| // square brackets, then convert that param into a Query object and continue | |
| // parsing recursively, returning an empty object when there are no params to | |
| // parse | |
| export type RouteParamsObjectFromPath<Path extends string> = | |
| Path extends `${infer Prefix}[${infer Param}]${infer Suffix}` | |
| ? RouteParamsObjectFromPath<Prefix> & // recurse on prefix | |
| AsQuery<Param> & // convert param into RouteParams | |
| RouteParamsObjectFromPath<Suffix> // recurse on suffix | |
| : {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
now we can define a method that take the path, and a handler, and ensures that the query the handler receives has the parameters referred to by the path string:
in practice, you will find that
abcbelow, will result in a TypeScript error: