Skip to content

feat: make typed useRoute(currentRouteName) return children route types as well #2475

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

Merged
merged 10 commits into from
Apr 25, 2025

Conversation

Anoesj
Copy link
Contributor

@Anoesj Anoesj commented Mar 13, 2025

In an app (with a typed router) where route /a/b is a child of route /a, this makes useRoute('/a') return RouteLocationNormalizedLoadedTyped<RouteNamedMap, "/a"> | RouteLocationNormalizedLoadedTyped<RouteNamedMap, "/a/b">.

It works in a recursive manner, so whenever route /a/b/c is added as a child of /a/b, useRoute('/a') will return RouteLocationNormalizedLoadedTyped<RouteNamedMap, "/a"> | RouteLocationNormalizedLoadedTyped<RouteNamedMap, "/a/b"> | RouteLocationNormalizedLoadedTyped<RouteNamedMap, "/a/b/c">`.

Of course useRoute('/a/b/c') will only return RouteLocationNormalizedLoadedTyped<RouteNamedMap, "/a/b/c">.

Note

This PR is intended to be paired with the following unplugin-vue-router PR: posva/unplugin-vue-router#602
That PR changes the DTS generation to include RouteMeta and a union of the children route's names as generics to every RouteRecordInfo type.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…ypes as well
Copy link

netlify bot commented Mar 13, 2025

Deploy Preview for vue-router canceled.

Name Link
🔨 Latest commit bc9cea2
🔍 Latest deploy log https://app.netlify.com/sites/vue-router/deploys/680b409cc34892000806e8c2

posva added 3 commits April 23, 2025 14:49
Copy link

pkg-pr-new bot commented Apr 23, 2025

Open in StackBlitz

npm i https://pkg.pr.new/vue-router@2475

commit: bc9cea2

Copy link

codecov bot commented Apr 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.89%. Comparing base (3753ede) to head (bc9cea2).
Report is 15 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2475   +/-   ##
=======================================
  Coverage   94.89%   94.89%           
=======================================
  Files          34       34           
  Lines        2998     2998           
  Branches      845      845           
=======================================
  Hits         2845     2845           
  Misses        150      150           
  Partials        3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

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

I think this is ready!

posva added 4 commits April 25, 2025 09:51
@@ -17,16 +13,30 @@ export interface RouteRecordInfo<
// TODO: could probably be inferred from the Params
ParamsRaw extends RouteParamsRawGeneric = RouteParamsRawGeneric,
Params extends RouteParamsGeneric = RouteParamsGeneric,
Meta extends RouteMeta = RouteMeta,
Copy link
Member

Choose a reason for hiding this comment

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

I removed this type param because it was never used and I figured it's safer to add it back later if needed than leaving an unused type param that can eventually be used. I marked this as a fix because the leftover type param should have never been released in the first place

@posva posva merged commit 0ce408d into vuejs:main Apr 25, 2025
5 of 8 checks passed
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.

None yet

2 participants