statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
26.53k stars 1.22k forks source link

@xstate/fsm: `TEvent` always includes `InitEvent` #4469

Open pokey opened 7 months ago

pokey commented 7 months ago

I was defining a simple state machine, and noticed that the InitEvent seems to always be an expected event according to the type system, when in fact, from what I can tell, it only makes sense as an entry event for the initial state.

In the following code:

import { ActionType } from "@cursorless/common";
import { createMachine } from "@xstate/fsm";

interface ActionEvent {
  type: "action";
  actionName: ActionType;
}
type Event = ActionEvent;

type Context = object;

interface InitialState {
  value: "initial";
  context: Context;
}
type State = InitialState;

createMachine<Context, Event, State>({
  initial: "initial",
  states: {
    initial: {
      on: {
        action: {
          actions: (_, { type }) => {
            console.log(type);
          },
        },
      },
    },
  },
});

See the type hint

image

I would expect it to just be type: "action".

I believe this line causes the problem:

https://github.com/statelyai/xstate/blob/e87600ea450fa19b71d2051fac3c6c54fb9d486e/packages/xstate-fsm/src/types.ts#L77

Andarist commented 7 months ago

I agree, | InitEvent should be added "on demand" by the consumer of the type when needed. It shouldn't be hardcoded in the ActionFunction type

davidkpiano commented 7 months ago

I'd appreciate a PR for this one if you'd like to fix it @pokey

LookRain commented 1 week ago

@davidkpiano hey David, what's the teams plan for the fsm package? i see it's already removed from the master branch, is it considered deprecated now?