Generic over states and transitions #11
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR for my suggestion under your reddit post.
Type changes:
any
types andas
casts (except for one). This means that everything is now verified by TS.KeysOfTransition
helper type. TypeScript can infer all state and transition types, so this helper is no longer necessary.BaseStateConfig
andBaseConfig
. These were purely internal types. I don't know why you added them in the first place. I just usedMachineStateConfig
andMachineConfig
everywhere.useStateMachine
is no longer constrained. From what I see, this only got in the way of TS inferring the type. I'll re-add it if it's important.useStateMachineWithContext
is now generic over states and transitions and not over the config type. This allows TS to infer all states and transitions. However, this is a breaking change for TS users.Behavioral changes:
reducer
: I changed thecurrentState?.on
tocurrentState.on
. Since the state machine is now type-checked, users will get a compilation error if they try to transition into an undefined state. ThereforecurrentState
must be non-nullable.{}
. This was actually a bug of the old implementation. I fixed this bug here because TS would not compile (that's the advantage of not usingany
andas
:) ). Example:Feel free to request changes.
You can try out everything here.