-
Couldn't load subscription status.
- Fork 295
feat: improve param type inference #1217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
0471273 to
a46b3c6
Compare
|
This is an awesome start. Wondering if we could pair it with |
Yea, i have already started working on it locally. But ran into some beahviour issues which i am trying to figure out first 👍🏻 Will include it in this PR when i am done. |
|
@pi0 This PR should be ready for a quick review when you have a moment - happy to adjust anything if needed 😊 I have tried cleaning the overloads from |
|
Sorry, it got delayed @luxass, we will try to review soon (also added @danielroe) |
* Updated the `H3EventContext` interface to accept a generic type parameter `TParams` for `params`. * This change enhances type safety and flexibility for router parameter handling.
* Updated the `context` property in `H3Event` to infer `routerParams` more effectively. * Added tests to verify the inference of router parameters from `EventHandlerRequest`. * Ensured default behavior for cases without specified `routerParams`.
This feels cleaner to have the optionality of the params in the context.
…nction signatures
The previous implementation in the pull request, removed access to the optional properties.
* Added `RouteParams` type to simplify route parameter inference. * Updated `on`, `get`, `post`, `put`, `delete`, `patch`, `head`, `options`, `connect`, and `trace` methods to utilize the new `RouteParams` type for better type safety. * Improved type definitions for event handlers to include inferred route parameters.
This will hopefully clean the types up a bit.
* Removed redundant `const` keyword from route type parameters in `on`, `get`, `post`, `put`, `delete`, `patch`, `head`, `options`, `connect`, and `trace` methods. * Improved type inference for route parameters.
This should make the h3 declared class not look too complex with the overloads. I couldn't get the `all` to use the H3HandlerInterface, some type errors in H3Core appeared 😅
537000c to
931281a
Compare
|
Dear @luxass you don't need to rebase PR on all commits. I can take care of rebase before merge 👍🏼 |
This PR resolves #1053 by adding automatic type inference for route parameters. When you define a route with parameters using
app.get(),app.post(), or any other HTTP method, TypeScript now knows exactly what parameters are available inevent.context.params.Previously,
event.context.paramswas always typed asRecord<string, string> | undefined, even when the route pattern clearly defined specific parameters. Now the route pattern is parsed at the type level to extract parameter names and provide full type safety.Route parameters are now fully typed based on the route pattern you define:
This works with multiple parameters too:
The existing helper functions (
getRouterParamandgetRouterParams) also benefit from this:Type inference works across all route registration methods like
app.get(),app.post(),app.put(),app.delete(), andapp.on(). Routes without parameters haveparamstyped asundefined, so you'll know when there are no parameters available.When you use
defineHandlerdirectly (outside of a route), the route pattern isn't available yet, so params remain untyped asRecord<string, string> | undefined. You can still manually type them using therouterParamsfield inEventHandlerRequestif needed. However, when you inlinedefineHandlerwith a route, the params are fully typed automatically:The implementation leverages
InferRouteParamsfrom rou3 (h3js/rou3#168) for route pattern parsing. I have tried to make the types backward compatible, so if you catch something that doesn't work as before, just tell me and i'll fix it 😅