Skip to content
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

Typescript suggestions based on router configuration #2308

Closed
rsmple opened this issue Jul 22, 2024 · 1 comment
Closed

Typescript suggestions based on router configuration #2308

rsmple opened this issue Jul 22, 2024 · 1 comment

Comments

@rsmple
Copy link

rsmple commented Jul 22, 2024

What problem is this solving

Working with vue-router on different projects can be difficult because of the complicated route structure. It is not obvious which routes exist in the current configuration and what parameters they require (before you get an error at runtime). Verifying route locations becomes even more difficult when the router's configuration is spread across many files.

This feature should help to create and validate route locations across the project without much effort.

Proposed solution

Actually it is already possible to enable such TypeScript suggestions by declaring all router records with as const satisfies RouteRecordRaw[] and processing the result type. Here I wrote an article on how to implement this in Vue project. But this approach overrides internal interfaces and may noy be reliable enough. I think it is possible to implement this on the vue-router side, and that would work even better.

I would also like to suggest generic syntax for useRoute():

type RouteName // all existing route names

...

const route = useRoute<RouteName.SOME_ROUTE>() // route location, based on router record for route with name RouteName.SOME_ROUTE

This could also be done using the suggested router record processing.

Describe alternatives you've considered

No response

Copy link
Member

posva commented Jul 23, 2024

This solution was initially implemented but turned out to crash ts with enough routes. Instead, we added #2264 and you can use https://uvr.esm.is/ for typed routes. There is an ongoing refactor of the internal matchers that could simplify some of the types to make them viable but try unplugin-vue-router, you can also add routes at build time to have types without using the file-based routing

@posva posva closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2024
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

No branches or pull requests

2 participants