Skip to content

Fix route match and router route types to fix narrowing and type guard issues#704

Merged
pleek91 merged 3 commits intomainfrom
fix-route-types-and-narrowing
Feb 17, 2026
Merged

Fix route match and router route types to fix narrowing and type guard issues#704
pleek91 merged 3 commits intomainfrom
fix-route-types-and-narrowing

Conversation

@pleek91
Copy link
Copy Markdown
Contributor

@pleek91 pleek91 commented Feb 16, 2026

Description

This PR fixes two bugs which both were causing issues with narrowing router.route either by narrowing the route itself or when using useRoute or isRoute.

Match widening to string

Because CreateRouteOptions['name'] is type string when a route had no name (either by being omitted or by passing an empty string) then Route['matches'][number]['name'] would get widened to string. Would would cause issues for any routes with a no name parent.

To fix this I've implemented the toName both in the types and in the match created within createRoute.

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, and id is added. So I think having a default value for name also makes sense here.

I did attempt to fix this without modifying the match types and just fixing it within the RouteWithMatch type but that proved difficult (I could get one test case passing but not another).

router.route including no name routes

router.route is 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 RouterRouteUnion which 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.

@netlify
Copy link
Copy Markdown

netlify bot commented Feb 16, 2026

Deploy Preview for kitbag-router ready!

Name Link
🔨 Latest commit aa26c85
🔍 Latest deploy log https://app.netlify.com/projects/kitbag-router/deploys/6993afb9a67b2b00086ba20a
😎 Deploy Preview https://deploy-preview-704--kitbag-router.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pleek91 pleek91 force-pushed the fix-route-types-and-narrowing branch from a2947a4 to aa7a9de Compare February 16, 2026 23:56
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes didn't seem to effect anything but I do think this is probably good to have.

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 })
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pleek91 pleek91 merged commit da1ddaa into main Feb 17, 2026
6 checks passed
@pleek91 pleek91 deleted the fix-route-types-and-narrowing branch February 17, 2026 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants