Fix route match and router route types to fix narrowing and type guard issues#704
Merged
Fix route match and router route types to fix narrowing and type guard issues#704
Conversation
✅ Deploy Preview for kitbag-router ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
a2947a4 to
aa7a9de
Compare
pleek91
commented
Feb 16, 2026
Contributor
Author
There was a problem hiding this comment.
These changes didn't seem to effect anything but I do think this is probably good to have.
pleek91
commented
Feb 17, 2026
| const context = options.context ?? [] | ||
| const { store, ...hooks } = createRouteHooks() | ||
| const rawRoute = markRaw({ id, meta, state, ...options, props }) | ||
| const rawRoute = markRaw({ ...options, id, meta, state, props, name }) |
Contributor
Author
There was a problem hiding this comment.
I added name and moved options to the beginning. I don't believe moving options to the beginning has any real effect, but this makes sure that the normalized consts will always override anything passed in as options. For example if an id value was passed by a user now it will be ignored. Types would prevent this but we don't want users hacking these sorts of things.
stackoverfloweth
approved these changes
Feb 17, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes two bugs which both were causing issues with narrowing
router.routeeither by narrowing the route itself or when usinguseRouteorisRoute.Matchwidening tostringBecause
CreateRouteOptions['name']is typestringwhen a route had no name (either by being omitted or by passing an empty string) thenRoute['matches'][number]['name']would get widened tostring. Would would cause issues for any routes with a no name parent.To fix this I've implemented the
toNameboth in the types and in the match created withincreateRoute.Originally I avoided this because generally the "match" type directly reflects what the user passed in. But we're already adding default values for
meta,state, andidis added. So I think having a default value fornamealso makes sense here.I did attempt to fix this without modifying the match types and just fixing it within the
RouteWithMatchtype but that proved difficult (I could get one test case passing but not another).router.routeincluding no name routesrouter.routeis a union of all of the route possibilities that could be matched by the router. However this was including no name routes. Which could produce some weird types when doing route narrowing.To fix this I updated the existing
RouterRouteUnionwhich previously was just a typescript hack to make a distributive type to also filter out routes with no name which makes the type more accurate and also gives it a meaningful purpose.