Replies: 7 comments
-
This is already, somewhat, on our roadmap (exhaustive type generation). It will take some time before we deliver this though as we are focused on some other work right now. Would you maybe be interested in helping us out with this? |
Beta Was this translation helpful? Give feedback.
-
@Andarist, sure, do you have a particular vision on how something like this should be done? Or any proof of concept would suffice for now? |
Beta Was this translation helpful? Give feedback.
-
I have not yet thought much about technical details on how this could be implemented. I think it would be the best to reuse existing tooling as much as possible, so I would start with exploring ESLint + TypeScript programmatic APIs to see what's possible. Most likely we'd need to actually write generated types onto the file system to make TS actually type check against that generated schema. Any proof of concept would be good - you can also reach out to me on Twitter or some other channel so we could discuss this further if you want. |
Beta Was this translation helpful? Give feedback.
-
@Andarist, progress report. Finally been able to play with xstate a little more on this one. I've made a repo here for now: I entertained the idea of generating a ts-machine-module from a simple machine config in JSON, using xstate-machines 😋. The example here is just a simple tree-traversal machine Here's the resulting module: Currently, it generates:
And here's how to consume a generated module (with react): There's still probably a lot more stuff possible to do with just a simple JSON-schema, but complex stuff requires a source to be typed as well 🤔 Thoughts? |
Beta Was this translation helpful? Give feedback.
-
@MrNovado I just want to let you know that I've seen this! Thanks for doing this ❤️ . I'm still thinking about various things regarding the whole topic so I can make sure that I give a thoughtful comment on this. |
Beta Was this translation helpful? Give feedback.
-
@MrNovado this is a really cool experiment! It IMHO showcases nicely that this can be done. The one problem I have with the used approach is that it requires defining schema separately and I believe that the experience of authoring machines should not change (not by much at least). Otherwise, we force people to learn more APIs, we need to document more, etc. Schemas are useful, but for the ideal developer experience, this should be as seamless as we can make it. At least that should be the default - more capabilities can, of course, be added. After thinking about it some more - I think we could start from writing a dedicated VScode plugin that would generate those typings based on existing configs passed to The tricky part (which I have not figured out yet) is how to trick TS compiler to use those generated types instead of builtin ones. We definitely need to alter the source code, otherwise, TS won't be able to resolve and type-check this. But how to do it? What's the user input? What's the generated output? One, very rough, possibility which comes to my mind is this: import { createMachine } from 'xstate'
+import { LightMachineType } from './light-machine.xstate-types.ts'
const lightMachine = createMachine({
initial: 'green',
/* ... */
-})
+}) as any as LightMachineType There are also some unknowns here. For example - how actual work in IDE will look like when updating the input machine? Should we try to regenerate types when writing? Should we only regenerate on save? If latter - won't it be confusing to see errors reported based on the outdated types before user hits save? |
Beta Was this translation helpful? Give feedback.
-
Technically,
I personally think it is very much inevitable. Type-enhanced schemas, by definition, provide new Trying it out and seeing how actually
Plugin or a library -- to me it feels like a delivery method more than anything. IDE integration seems like a neat feature for those who have the product, but a library-package is a lot more universal in terms of usability (or accessibility even). VScode is a nice tool indeed, but I fail to see the necessity of funneling your userbase into this specific product in such a way.
As for the generated output, the usual case with generators, at least from my experience, is to output stuff in a separate module (with a Hot-schema-regeneration is a neat QoL, but is not required at first and still can be introduced later on. There's no need to complicate this so much. Let's just make a library-package tool that generates an enhanced schema in some way, with some Cheers, |
Beta Was this translation helpful? Give feedback.
-
So I've been playing around with TS and XState for a while now and... I have to say, the value TS brings to the table here for the end-user is very limited.
Like, it's cool to be able to somewhat competently build schema' artifacts, and see actions' descriptors and whatnot, but when it comes to implementing a schema into a machine -- you feel almost completely disconnected. Working with big-enough schemas feels unsafe and forces you to grock and glance over the schema over and over until implementation is 'done'.
Questions which are very hard to answer without looking at the schema of your machine:
(state, event)
) for the selected action/condition? (currently assumes all actions could be executed at any state-node)Proposal
In order to answer at least some of those questions and enhance the overall dev-experience, it seems one have to up the abstraction level and generate machine-schema and all the helpers (something like folks from apollo[graphql-impl] did) first, before consuming.
https://github.com/MrNovado/xstate-gentype-rfc
What do you think?
In my proposal, I used a jsx-like syntax to show how the machine-description might look like, but the syntax could be exactly the same as the one xstate already uses (or anything else really). Moreover, machine-description could be generated from a visual constructor of sorts, like folks from yakindu have shown.
Beta Was this translation helpful? Give feedback.
All reactions