nextjs-routes
nextjs-routes copied to clipboard
Allow href in Links to be a string
Since Next allows href
to be a string in Link
s, 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 :)
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 so changing href: RouteOrQuery;
to href: Route["pathname"] | RouteOrQuery;
results in this error message, which I think is clear enough. What do you think?
Yeah that error message seems clear enough. Is it roughly similar with a bad RouteOrQuery object?
Hmmm no, in that case we have the error message that you described:
However, this seems to be the case with and without my type change.
Hmm even with a route closer to a valid defined route? I wonder if we regressed with the introduction of RouteOrQuery.
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 :)
Yes continuing with the implementation sounds great, thanks
Implemented in #84 :)