import * as React from 'react' ; type Machine < S > = { [ k ] : { [ k ] : S ; } ; } ; type MachineState < T > = keyof T ; type MachineEvent < T > = keyof UnionToIntersection < T [ keyof T ] > ; type UnionToIntersection < T > = ( T extends any ? ( x : T ) => any : never ) extends ( x : infer R ) => any ? R : never ; export function useStateMachine < M > ( initialState , machine ) { return React . useReducer ( ( state , event ) : MachineState < M > => { const nextState = ( machine [ state ] as any ) [ event ] ; return nextState ?? state ; } , initialState ) ; }