cassiozen / useStateMachine

The <1 kb state machine hook for React
MIT License
2.38k stars 47 forks source link

“send” parameter type is not narrow inside effects #4

Closed cassiozen closed 3 years ago

cassiozen commented 3 years ago

The "send" function argument type should be an union of transition names. It works properly in the send method returned by useStateMachine - but the send method passed to effect currently broadens to "string".

cassiozen commented 3 years ago

State might need to be a function to enable inference at individual state level. Maybe the API could look like this:

useStateMachine()({
  initialState: 'inactive',
  debugMode: true,
  inactive: stateNode({
    on: { TOGGLE: 'active' },
  }),
  active: stateNode({
    on: { TOGGLE: 'inactive' },
    effect() {
      console.log('Just entered the Active state');
    },
  }),
}
});
cassiozen commented 3 years ago

Working TS Sample with type narrowing on both send functions: typescript playground

cassiozen commented 3 years ago

Solved by #11