tatethurston / nextjs-routes

Type safe routing for Next.js
MIT License
571 stars 23 forks source link

Allow href in Links to be a string #83

Closed MariaSolOs closed 1 year ago

MariaSolOs commented 1 year ago

Since Next allows href to be a string in Links, it would be nice to also offer this more compact syntax (e.g.: Allow users to write href="/" instead of href={{ pathname: '/' }}). We can still make this typed so that only the corresponding string literals are allowed.

If this sounds like a good idea, I can create the PR with the implementation :)

tatethurston commented 1 year ago

Hey @MariaSolOs I had this in an early version of nextjs-routes but the TypeScript error messages had poorer DX. Any typos in routes would present an error message using the first member of the routes union rather than the most similar member. This was several months ago so if the TS inference has improved in more recent versions or if you can find a way to express the type differently that helps TS yield a better error message (maybe a conditional type with the string and object variants instead of a single union?) I’m open to it. Also happy to support this via a config option if we can’t overcome the error messaging DX.

MariaSolOs commented 1 year ago

@tatethurston so changing href: RouteOrQuery; to href: Route["pathname"] | RouteOrQuery; results in this error message, which I think is clear enough. What do you think?

image
tatethurston commented 1 year ago

Yeah that error message seems clear enough. Is it roughly similar with a bad RouteOrQuery object?

MariaSolOs commented 1 year ago

Hmmm no, in that case we have the error message that you described:

image

However, this seems to be the case with and without my type change.

tatethurston commented 1 year ago

Hmm even with a route closer to a valid defined route? I wonder if we regressed with the introduction of RouteOrQuery.

MariaSolOs commented 1 year ago

It seems like the error always shows the same route.

Given that this regression isn't caused by my proposed change, are you okay with me continuing with the implementation, or would you like to first fix this?

I'm okay either way :)

tatethurston commented 1 year ago

Yes continuing with the implementation sounds great, thanks

MariaSolOs commented 1 year ago

Implemented in #84 :)