-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
feat: experimental typed routes #3142
feat: experimental typed routes #3142
Conversation
commit: |
src/gen.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow! This typing is too amazing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should reference the original source types π
I had to redefine/reimplement a bunch of internal types of vue-router
and uvr, so the credits for these go to @posva
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just reviewed your PR!
It's an excellent for me!
Since it uses the vue-router type, it would be good if posva is able to review it. @posva |
@userquin |
src/internal-global-types.d.ts
Outdated
/** | ||
* Helper to generate a type safe version of the {@link RouteLocationAsRelative} type. | ||
*/ | ||
export interface RouteLocationAsRelativeTyped< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the same type exported by the router?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm most of the types should be the same except suffixed with I18n
, which this type seems to be missing. This might conflict with existing router types π«£ going to look into it.
Reasoning behind redefining the same types but suffixed is to use the RouteMapI18n
type, perhaps there's a better way to go about this that I'm unaware of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesnβt extending the same route map from Vue router works? Or do you need to have two separate route maps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're generating routes for each locale (depending on the routing strategy) and custom localized route the generated types are localized, this PR generates RouteMapI18n
(which ironically contains the routes without localization) to be used for the i18n routing utility functions.
Some examples to illustrate the problem we're trying to solve:
Path completion (not desirable when using custom routes)
I guess this is specific to i18n, I doubt there are other use cases where generating a separate map is necessary π€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would need to try it out to see the needed final types. I was hoping the types were flexible enough to at least keep things typer for the router methods. I18n methods could use extra types for localized route names (that seems to be the main difference)
π Linked issue
β Type of change
π Description
Important
Very experimental and relies on nuxt/router/uvr internals, this will not be backported to v8.
I'm not very happy with the implementation right now, but it does work... I think the demand/convenience of this feature somewhat justifies adding this albeit under an experimental flag.
We should communicate very clearly that this relies on internals of Nuxt, Vue Router and
unplugin-vue-router
, perhaps we can make the types fallback to original behavior on failure (maybe it already does). A proper implementation will likely require collaboration with the maintainers of those packages, we should not expect this to keep working as is.This will need some documentation and basic tests so we know things fail if internals have changed.
Demo stackblitz project here: https://stackblitz.com/edit/bobbiegoede-nuxt-i18n-starter-srohiz?file=nuxt.config.ts&file=composables%2Ftest.ts
Demo of
localePath
autocompletionCode_2024-09-27_12-02-50.mp4
Demo of custom routes configuration autocompletion and basic validation (prefix
'/'
)Code_2024-09-27_12-06-59.mp4
π Checklist