statelyai / xstate-tools

Public monorepo for XState tooling
183 stars 36 forks source link

Support state and event as enum value #32

Open nicholas570 opened 2 years ago

nicholas570 commented 2 years ago

I'm using enums to type my machine states and events. It seem like the viz is only able to determine them when they are plain string

See above, [AuthStates.Forgot] is not in the viz, while idle , login and register are. Same for the events, as you can see from login state I can only receive register event

ext-enum
lowfront commented 2 years ago

I think this is necessary, too. We can only communicate with the machine through events in XState, but the event type is currently using as string type. I tend to use enum for convenience of management because the type of event can be used in both machines and communication. Think about when you just want to rename an event type for readability. Enum allows you to find and change where you used the event type. Otherwise, you must manually find and change the event type string.

When saving VSCode's XState Visual Editor, the editor changes the modified content in the code. However, actions created in line are excluded. Considering this method of operation, I think we can add this function by excluding keys entered by Computed property names from the code change.

davidkpiano commented 2 years ago

There are some unanswered questions with supporting a feature like this:

There's a lot of ambiguity that arises when trying to make Typegen "read your mind", as the number of potential ways a machine can be authored grow.

lowfront commented 2 years ago

I checked the link after leaving a comment. If I used Typegen, I was able to obtain the type estimation more comfortably and accurately than manually entering the type. I also overlooked that the editor can change the event name and state name. Unlike my previous comment, I think it's not a simple task.

I didn't use Typegen, but when I applied Typegen as shown in the link, I thought I didn't have to use enum. If you haven't used it, I recommend that you try it. @nicholas570

nicholas570 commented 2 years ago

@lowfront thanks, I'll try it 😉