Created
February 21, 2024 01:57
-
-
Save mateusduraes/fe8c3654bdb606da56b95f5c11629b43 to your computer and use it in GitHub Desktop.
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
const route = '/products/:id'; | |
type Split<S extends string> = S extends '' | |
? [] | |
: S extends `${infer C}${infer R}` | |
? [C, ...Split<R>] | |
: never | |
type RequestHandler<TRoute extends string> = { | |
params: TRoute extends `${infer _}:${infer R}` ? { | |
[key in R]: string | |
} : never; | |
} | |
type HandleRoute<TRoute extends string> = ( | |
request: RequestHandler<TRoute> | |
) => void; | |
const routes = { | |
'products/:productId': (request) => { | |
request.params.productId | |
}, | |
'items/:itemId/reviews': (request) => { | |
request.params.itemId | |
} | |
} | |
const handleProductId: HandleRoute<'products/:productId'> = (request) => { | |
request.params.productId | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment