nextjs-routes icon indicating copy to clipboard operation
nextjs-routes copied to clipboard

Allow href in Links to be a string

Open MariaSolOs opened this issue 2 years ago • 7 comments

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

MariaSolOs avatar Oct 22 '22 17:10 MariaSolOs

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.

tatethurston avatar Oct 22 '22 20:10 tatethurston

@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

MariaSolOs avatar Oct 22 '22 20:10 MariaSolOs

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

tatethurston avatar Oct 22 '22 21:10 tatethurston

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.

MariaSolOs avatar Oct 22 '22 21:10 MariaSolOs

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

tatethurston avatar Oct 22 '22 21:10 tatethurston

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

MariaSolOs avatar Oct 23 '22 01:10 MariaSolOs

Yes continuing with the implementation sounds great, thanks

tatethurston avatar Oct 23 '22 02:10 tatethurston

Implemented in #84 :)

MariaSolOs avatar Oct 23 '22 18:10 MariaSolOs