victorgarciaesgi / nuxt-typed-router

🚦Provide autocompletion and typecheck to Nuxt router
https://nuxt-typed-router.vercel.app
MIT License
362 stars 12 forks source link

Suggestion: Better helpers types #145

Closed LLCampos closed 7 months ago

LLCampos commented 7 months ago

Hi, :)

Project looks great!

I'm playing around with the helpers, but having some issues with the types.

For example, if I call:

const route = helpers.path({name: 'admin-id'})

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.

image
victorgarciaesgi commented 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

LLCampos commented 7 months ago

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 :)

victorgarciaesgi commented 7 months ago

Well helpers.path() is for ..paths. Like helpers.path("/home") you need helpers.route({name: "..."})

LLCampos commented 7 months ago

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 :)

victorgarciaesgi commented 7 months ago

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

victorgarciaesgi commented 7 months ago

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

LLCampos commented 7 months ago

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'.
victorgarciaesgi commented 7 months ago

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