statelyai / xstate

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

Bug: [Typescript] Failing to declare first parameter of guard breaks type inference in enqueueActions if another guard accepts dynamic parameters #5014

Open taraldr opened 3 months ago

taraldr commented 3 months ago

XState version

XState version 5

Description

Hi, love XState - just ported my teams typescript clients to XState, and came across this bug I think you might find interesting.

Parameter inference seems to break if you have:

  1. A guard which fails to declare the first parameter - () vs (_)
  2. Another guard which uses the second parameter - (_, params: { some: string )})
  3. An action using enqueueActions

This results in a typescript error on the action along the lines of, indicating that parameter type inference broke.

Type '{ type: "guardWithParams"; params: unknown; }' is not assignable to type '{ type: "guardWithParams"; params: { some: string; }; }'.

Workaround is always declaring the first parameter of all guards, even if unused.

@Andarist

Expected result

No type error.

Actual result

Type error.

Reproduction

https://stackblitz.com/edit/vitejs-vite-dwgz2w?file=src%2Fmain.ts&view=editor

Additional context

v5.16.0

taraldr commented 3 months ago

Also observing the same behaviour if you have:

  1. An action which fails to declare the first parameter
  2. Another action which uses the second parameter
  3. A third action using enqueueActions

Repro: https://stackblitz.com/edit/vitejs-vite-kemb2g?file=src%2Fmain.ts&view=editor