statelyai / xstate

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

Bug: type error when referencing guard with `check` inside `setup` #4834

Open with-heart opened 6 months ago

with-heart commented 6 months ago

XState version

XState version 5

Description

The check function provided by enqueueActions can't be used to reference a guard by type or by parameterized object.

Expected result

I expected check to not cause a type error when referencing a guard.

Actual result

check causes a type error when referencing a guard.

Reproduction

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240331#code/JYWwDg9gTgLgBAbzgZwKYwK5gDR1QOwEcNUSBBAYxmAn2TgF84AzKCEOAIgA9kYBDGKk4BuOAHpxcGAE8wqZAC44AVgB0ATjUBGAFC60mMAAoEuuNLkLlZixYq0h3GDab96SGAAtg+AObK-PgyjNjmdqgAbgQwSohuHpbyypxYACaCwmLevgFwQSEMYRZF4fxUNHQ24RbpmQAqPv7KBMSkqJTUtMjGpnAUXqgUANaMAJRwALwAfIg1dsDMcMYDQ8PGnF7ujbkAwlv+qGmcYxO2diXzFovLqyN9sslcW8g7-vtBfkec42dXjPMGGNiqFwn4MPwoGk4ucLC83n4Poc0so+g58E4YLgojFxlNZrC7FB0BgoPh+o5UM41Dl-HAAISTSZ4aIYmlNPwiQEg0pArlAA

Additional context

Maybe related to the cause of #4820 but opening for tracking

Andarist commented 5 months ago

It's both the same as the other issue and different. Some of the underlying mechanisms are the same but it feels like this one should be easier for TS to infer - something is obviously missing in the implementation there.

Things break as soon as we include context-sensitive functions in guards. You can see how it works OK without them: TS playground

I highly doubt it's fixable on our side right now (I intend to work on this in the compiler but it won't happen too soon). Our current best bet to work around this would be to introduce this API: https://github.com/statelyai/xstate/pull/4703