Skip to content

Instantly share code, notes, and snippets.

@mateusduraes
Created February 21, 2024 01:57
Show Gist options
  • Save mateusduraes/fe8c3654bdb606da56b95f5c11629b43 to your computer and use it in GitHub Desktop.
Save mateusduraes/fe8c3654bdb606da56b95f5c11629b43 to your computer and use it in GitHub Desktop.
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