Closed LLCampos closed 7 months ago
This helpers are here to use where there is no autocomplete, for exemple in a computed
, where you want to return a link that will be used in NuxtLink
Yeah. In my case I have a function that returns a path we should redirect the user to. I was looking to use nuxt-typed-router
to do the "type checking" of the paths, like so:
const redirectUserTo = () => {
if (...) {
return helpers.path({name: 'admin-id'})
} else if {
return helpers.path({name: 'other'})
}
(...)
}
But then I had the types problem described above :)
Well helpers.path()
is for ..paths. Like helpers.path("/home")
you need helpers.route({name: "..."})
Sure, but note that that was just pseudo-code to describe my use case and why it would be useful to have more ergonomic types. :) What is returned by that redirectUserTo
function is not really usable.
Of course, its fine if this is out of scope or too hard to do :)
Ah ok! You mean the returned type is not correct to use with NuxtLink or navigateTo
? I'll look, do you have a screenshot of the error?
It was mean to work but maybe there is a regression
Tried but it works with NuxtLink But I think you misuse the helper. It's meant to be used where it's not possible to have autocomplete, it's not a navigation tool.
Image a computed like this
const userLink = computed(() => {
if (admin) {
return '/admin/user'
} else {
return "/user"
}
})
It becomes this:
const userLink = computed(() => {
if (admin) {
return helpers.path( '/admin/user')
} else {
return helper.path("/user")
}
})
It's just returns the same string or route object you provided
It's just returns the same string or route object you provided
Hm... what about this example?
import { helpers } from '@typed-router'
function getPath(): string {
return helpers.path('/admin')
}
Typescript complains with
Type 'Required<TypedRouteLocationRawFromName<never, "/admin">>' is not assignable to type 'string'.
Type 'Required<Omit<RouteLocationPathRaw | RouteLocationNamedRaw, "name" | "params" | "path"> & { name?: undefined; }>' is not assignable to type 'string'.
Yep that's the purpose, the helpers return a strict type corresponding to the route, to allow using it with any typed NuxtLink, router etc...
Typing the return type as string lose all the purpose of the module.
If you still want to tall typescript to not report error do return helpers.path('/admin') as string
Hi, :)
Project looks great!
I'm playing around with the helpers, but having some issues with the types.
For example, if I call:
The returned type is
Required<TypedRouteLocationRawFromName<RouteNameFromPath<T>, T>>
Its unclear how to use this type. For example, its doesn't offer anything useful in auto-complete.