8 ms·
Every time I hear any news about routing in a React framework I always get disappointed that the news isn't that they're introducing type-safe (TypeScript) rout
by amcsi 4y ago
Every time I hear any news about routing in a React framework I always get disappointed that the news isn't that they're introducing type-safe (TypeScript) routes to enable type-safe linking.
- onion2k 4y agoI'm curious about what you mean. There's no way to embed type information into a URL - every GET parameter is a string. Are you suggesting that visiting a link like /123 would go to the 'string: "123"' route, while clicking a <Link to={123}> would route you to 'number: "123"'? That would make deep linking in an app really hard to manage. I'm struggling to understand why you'd want to overload routes like that.
- kall 4y agoI'm pretty sure what they are looking for is that something like a Link that takes a route name and a params object that are both typed.
- gherkinnn 4y agoGenerating a set of links from the routing as a union of strings as type Route. And a <Link /> component that only accepts an href of that type. Like so: https://www.typescriptlang.org/play?jsx=4#code/C4TwDgpgBASg9gV2NAvAWAFBSgHygIgHp9Ns8iATAQwGcALAIzioCcKStcDDr6nWKhKhQC2ASwB2HMt16NmbQnABuEFsrEQA7tK4ADHrXkCeEYFTEAbQgBIA3hIQiGagL57S+w3wWCKZi2t7R2c3QhE4FggPTnJCGjNgSQBzGg5MAGM4CRpgKCoALlhEZCgUWSN+RX9zK0IARgAmAGYOLJy8hiL4JFQKnxMawIaW8MiIdIx23KgAGUkAazKoAAo7DLorCiiJABooOiiAM1ci9c3LbYgJbogqDOAAOTh-fcOII+6SiFcASlv7k8XqgAHyrTwAHioB2OKDs7xOIPOWx2rghQhBmF+QA https://www.typescriptlang.org/play?jsx=4#code/C4TwDgpgBASg9... TS' string literal types are powerful. https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types https://www.typescriptlang.org/docs/handbook/2/everyday-type... edit: I did implement this a while back but found it gave me only so much additional value. Though I can imagine that large applications with dozens of developers might indeed benefit from type-safe routes to avoid dead links.
- onion2k 4y agoThat's an interesting idea. Cheers.
- rozenmd 4y agoThis may be of interest: https://www.npmjs.com/package/hono https://www.npmjs.com/package/hono
- zoontek 4y agoShameful ad for our own router: https://swan-io.github.io/chicane https://swan-io.github.io/chicane, with type-safe route consumption and creation (and other goodies). No weirdness or magic API, the useRoute hook just return a discriminating union with typed params. It's fully-featured and used in prod. The API is quite simple: const Router = createRouter({ UserDetail: "/users/:userId" }); Router.UserDetail({ userId: "xxx" }); // safe