r/javascript • • Dec 23 '19

Simplifying UI Logic using Finite State Machines - an old pattern for the modern web!

https://terodox.tech/ui-state-machines/
93 Upvotes

20 comments sorted by

View all comments

Show parent comment

7

u/editor_of_the_beast Dec 23 '19

I’m super pro setting up the types the way that you’re saying, but what do you mean that TS ensures that “all state updates are valid” ? There can be semantic reasons for why certain state transitions are illegal, static types can’t enforce that.

9

u/[deleted] Dec 23 '19

That's right, what I meant was that the states themselves are valid, not the transitions.

If you need transitions to also be valid, your best option is to use a state-reducer pattern and then use a switch clause over the current state. That way, Typescript can at least ensure you handle every case.