feat(types): add ExtractRouteParams and route param inference#1327
feat(types): add ExtractRouteParams and route param inference#1327productdevbook wants to merge 2 commits intomainfrom
Conversation
Add ExtractRouteParams<Path> utility type that extracts parameter names
from route path strings at compile time:
type P = ExtractRouteParams<"/users/:id/posts/:slug">
// { id: string; slug: string }
Route methods (get, post, put, etc.) now infer route params from the
path string literal and pass them to the handler's event type. Combined
with the updated getRouterParams (now uses InferEventInput), this gives
automatic type inference:
app.get("/users/:id", (event) => {
const params = getRouterParams(event);
// params is { id: string } — inferred from path
});
Zero runtime changes — purely type-level improvement.
Closes #1053
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR implements route parameter type inference for the H3 framework. It introduces utility types to extract route parameters from route strings (e.g., Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~35 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
|
Closing — PR #1217 by @luxass already covers this with a more comprehensive approach:
Apologies for the duplicate work — should have checked open PRs more carefully. |
Summary
Closes #1053
Adds compile-time route parameter inference from path strings — zero runtime cost, purely type-level.
Before
After
ExtractRouteParamsutility typeAlso exported as a standalone utility:
Changes
src/types/_utils.ts: AddExtractRouteParams<T>utility type with_Prettifyfor clean intersection flatteningsrc/types/h3.ts: Add generic overloads to all HTTP method declarations (get,post, etc.) that infer path params and pass narrowed event to handlersrc/utils/request.ts: UpdategetRouterParamsto useInferEventInput(matchingreadBody/getQuerypattern) so params flow from event genericsrc/index.ts: ExportExtractRouteParamstypeDesign decisions
HTTPHandleroverloadRecord<string, string>(not{}) to handle catchall/wildcard paramsstring extends Tguard — when path is a plainstring(not a literal), falls back toRecord<string, string>Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
/users/:id/posts/:slug) automatically infer typed parameters (e.g.,{ id: string; slug: string }), improving developer experience and reducing runtime errors.