statelyai / xstate

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

Bug: type disagreement between emitted events and toPromise #4931

Closed boneskull closed 5 months ago

boneskull commented 5 months ago

XState version

XState version 5

Description

After #4905, there's some problem using toPromise on an actor created from a machine where types.emitted is specified.

Example machine:

const machine = setup({
  types: {
    emitted: {} as {type: 'FOO'}
  }
}).createMachine({
  initial: 'init',
  states: {
    init: {
      entry: [
        emit({type: 'FOO'})
      ],
      always: 'done'
    },
    done: {type: 'final'}
  }
});

Usage:

const actor = createActor(machine).start();
await toPromise(actor) // <-- TS error

Expected result

Would not expect a TS error here.

Actual result

(See playground

Argument of type 'Actor<StateMachine<MachineContext, AnyEventObject, {}, never, never, never, never, "init" | "done", string, NonReducibleUnknown, NonReducibleUnknown, { ...; }, MetaObject, ResolveTypegenMeta<...>>>' is not assignable to parameter of type 'AnyActorRef'.
  Types of property 'on' are incompatible.
    Type '<TType extends "FOO" | "*">(type: TType, handler: (emitted: { type: "FOO"; } & (TType extends "*" ? {} : { type: TType; })) => void) => Subscription' is not assignable to type '<TType extends string>(type: TType, handler: (emitted: EventObject & (TType extends "*" ? {} : { type: TType; })) => void) => Subscription'.
      Types of parameters 'type' and 'type' are incompatible.
        Type 'TType' is not assignable to type '"FOO" | "*"'.
          Type 'string' is not assignable to type '"FOO" | "*"'.

Reproduction

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgUxMGAaOBnZMCuYWMEAClBGrlgMZTICGMyAgjSVHAL5wBmFIOAHIAHthhNkQgNwAoWTQgA7cXBAMaAC2BLkcALw48hABQJZcODACeYZNgBciC5ZRoYzACZOEPBtkQbOychADEAeXChLhcYrgBKADo6RmYAWQ1tXTMXHXRgBgAbELyYIQwXcUlHZ1c4Up8XOstkJRgoaycAbSbmutR0MyDkEIiohN66gF0KvssigHcGaxqhT2UpXq5Z13XdH2GQ3h0i6NjZBLkFZVUNDgM4FMk2DhN1LR1kJKrYE3j5EjkSjAXAmO7Qf6yIA

Additional context

No response