State machines are a nice pattern, but true and false could also make up the entirety of states of a simple state machine. You don't need to make an enum of them.
Although there is no possibility to create an algebraic data type in TypeScript, we could artificially simulate it using a combination of classes and type discrimination in order to get a state that is both predictable, warning impossible states and the possibility to embed state in our "types" (that are really classes in a union).
classFormIdleState{}classFormSubmittingState{publicconstructor(publicreadonlyemail:string,publicreadonlypassword:string){}}classFormErrorState{publicconstructor(publicreadonlyerror:Error){}}classFormSubmittedState{}typeState=FormIdleState|FormSubmittingState|FormErrorState|FormSubmittedStatefunctionhandleState(state:State){if (stateinstanceofFormIdleState){alert("Please, login in order to access this app");// @ts-expect-error Just for the demo, but this is not possibleconsole.log(state.email);return;}if (stateinstanceofFormSubmittingState){alert(`Loggin with email ${state.email}`);// TODO: send an HTTP request// @ts-expect-error Just for the demo, but this is not possibleconsole.log(state.error.message);return;}if (stateinstanceofFormErrorState){alert(`There has been an error: ${state.error.message}`);// @ts-expect-error Just for the demo, but this is not possibleconsole.log(state.email);return;}alert("Successfully logged in");// TODO: redirect to profile page// @ts-expect-error Just for the demo, but this is not possibleconsole.log(state.email);}// Change me!conststate:State=newFormSubmittingState("email@domain.com","p4ssw0rd");handleState(state);
Note that this example uses TypeScript in order to prevent most human mistakes.
In my opinion, having ADT in TypeScript would remove the need for state machines.
at this point, you should consider using OOP instead of ADTs to solve this, like move the ifs to the respective classes as methods in order to implement something like the State pattern, making the code less verbose.
Graduated in Digital Media M.Sc. now developing the next generation of educational software. Since a while I develop full stack in Javascript using Meteor. Love fitness and Muay Thai after work.
State machines are a nice pattern, but
true
andfalse
could also make up the entirety of states of a simple state machine. You don't need to make an enum of them.Can you do a demo so I can refer to the code ?
Thank you sir -
I added a code example in the Exceptions section of the article. :)
I see the author added a part "the exception" and I welcome the addition.
Although there is no possibility to create an algebraic data type in TypeScript, we could artificially simulate it using a combination of classes and type discrimination in order to get a state that is both predictable, warning impossible states and the possibility to embed state in our "types" (that are really classes in a union).
Note that this example uses TypeScript in order to prevent most human mistakes.
In my opinion, having ADT in TypeScript would remove the need for state machines.
Definitely one way to look at it. Sadly it's too verbose in TS.
at this point, you should consider using OOP instead of ADTs to solve this, like move the ifs to the respective classes as methods in order to implement something like the State pattern, making the code less verbose.
Nice write! What's software you use to make that diagram?
Anyway, I also write article about simplifying complex state in React with reducer :)
Simplify state with reducer
M. Akbar Nugroho ・ Mar 8 '23
I'd also like to know the tool to model the state machines.
stately.ai
stately.ai
Very interesting, thanks for writing!
Thank you !
Good one
Thank you 🙏
I love the concept of a "state machine" for its simplicity and ease of understanding.
Great it's too help Full Pentagon Detailing...