Vague sweeping question about TypeScript and XState #4812
Replies: 3 comments 1 reply
-
Been there, done exactly that ! |
Beta Was this translation helpful? Give feedback.
-
I've landed on the other side of this (for now), with the result that I have somewhat less than the full type-safety that Xstate can provide, but I haven't relinquished the other benefits that you point to above (e.g., code organized in separate files: think I prefer the trade-off that I've made, though I'm relatively new to Xstate and I have little sense of the long-term implications of either direction. Please provide some guidance about what to consider when making these decisions, it would be very appreciated! |
Beta Was this translation helpful? Give feedback.
-
A helper function may be able to solve this. Think a Such a helper function may still be cumbersome--but that's the general solution I've seen to the problem of "omg too many type args". |
Beta Was this translation helpful? Give feedback.
-
This is kind of a rephrasing of my question in #4783, and is related to several other TS questions (#4697 for example).
The TypeScript story in v5 is a big improvement in most respects. If you use the
setup(...).createMachine(...)
API, and you put every single bit of your code between those parens, you get strong typing everywhere without any hassle.The tradeoff is that if everything has to be inline, you lose access to some of the language's fundamental tools for wrangling complexity - things like "assigning values to named constants" and "putting some of your code in other files".
This is not a criticism - I realize that getting type safety in this context is a hard problem, probably in ways that I only dimly understand; and I know you guys understand TypeScript better than pretty much anyone, and we're probably bumping up against the limitations of what TypeScript can do.
What I'm wondering is: Is there anything at all we can do to bridge the chasm that currently exists between easy
setup(...)
scenario, and any even-slightly-more-complex use case?To make this a bit less abstract, here's an example of what I'm talking about (see StackBlitz or playground):
Here I've almost succeeded at extracting some of the feedback machine's pieces - the config into a constant, and the action and the guard into standalone functions; but TypeScript is unhappy about
actions: { onSubmit }
:So I go looking for the type signature of
assign
, and it's this:...at which point I think most developers will do what I did, which is to throw up my hands and go back to putting everything inline.
setup()
use case?Again I hope this doesn't read as criticism, and I don't mean to whinge - I'd be very happy to contribute to a solution if you can give me a sense of how you're thinking about this. Or maybe I'm just holding it wrong and there's a straightforward way to do this, in which case I'd be happy to contribute to the docs.
Beta Was this translation helpful? Give feedback.
All reactions