Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Typed routes #1053

Open
eMerzh opened this issue Aug 16, 2023 · 2 comments
Open

[Feature] Typed routes #1053

eMerzh opened this issue Aug 16, 2023 · 2 comments

Comments

@eMerzh
Copy link

eMerzh commented Aug 16, 2023

Hello,

i know it's a long stretch, but i was dreaming about beeing able to do routing to named route (i know the existing side plugin)
but fully typed, like you couldn't route to an unexisting route verrifiied by typescript.
I know there are other router doing this and i was wondering if it could be possible :)

Thanks a lot for your work:)

@jquense
Copy link
Member

jquense commented Aug 20, 2023

Yeah I'd like to have this at some point but I'm not convinced it makes sense as part of the route config. We've been experimenting with something similar at my work and while it's really nice to generate urls from routes you immediately run into cyclical import issues, that don't necessarily cause problems but are still frustrating to deal with.

In terms of limiting found APIs to only accepting valid routes, you run into similar problems and ultimately the user needs to override some core type themselves to pass in valid routes, since hooks wouldn't really be aware of the route config without introducing even more spots for cycles.

"Other" routers I've seen try this, tend to overstate their type safety, they tend to not actually be much safer than the spa-routing library in the end. Overall yes I'd like to make improvements or recommendations here but not confident yet on the right way

@steinybot
Copy link

We have been using https://github.com/japgolly/scalajs-react/blob/master/doc/ROUTER.md which is a Scala.js library. Of course that is in Scala but it is fully type safe. I'm trying to switch to found-relay with the TS -> Scala.js type conversions and this is one of the things that I am missing (the other big ones are that the Component is Component?: React.ComponentType<any>; rather than having nicely typed props, and the other is that match.location.state doesn't make use of TState so it is any).

I am able to workaround most of it by creating my own inverted tree of configuration objects which point to their parent (so they can generate their route location) and then use these to generate the route configuration.

To get the added type safety in hooks you can't have a single context. You would have to create a new context when creating the router. I'm not sure if this would be worth it.

If the hooks took an extra type parameter for the route config used this would be a big win even if it was unsafe. You would only run afoul if you had multiple routers and specified the wrong type. Even then it's no worse than providing a location that doesn't match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants