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: guard helper typings may be broken #5046

Closed boneskull closed 3 months ago

boneskull commented 3 months ago

XState version

XState version 5

Description

5009 / 5.17.1 seems to befoul something related to guard helpers (and, not, etc.). On some machines using guard helpers, I see:

error TS2742: The inferred type of 'SomeMachine' cannot be named without a reference to
 '../../../../node_modules/xstate/dist/declarations/src/guards'. This is likely not portable. 
A type annotation is necessary.

This error is not present in 5.17.0.

Expected result

No type error

Actual result

Type error

Reproduction

n/a

Additional context

I don't have a reasonable reproduction yet; this does not affect every machine using the helpers and seems like an edge case. It might only happen once some threshold of machine complexity is reached.

Speculating: the type arguments for the helpers are too wide; e.g.:

export function not<
  TContext extends MachineContext,
  TExpressionEvent extends EventObject,
  TArg
>: GuardPredicate<
  TContext,
  TExpressionEvent,
  unknown,
  NormalizeGuardArg<DoNotInfer<TArg>>
>;

It may be it should look more like:

export function not<
  TLogic extends AnyStateMachine,
  TExpressionEvent extends EVENTS_FROM_TLOGIC,
  TArg
>: GuardPredicate<
  CONTEXT_FROM_TLOGIC,
  TExpressionEvent,
  unknown,
  NormalizeGuardArg<DoNotInfer<TArg>>
>;

WORKAROUND: This error goes away by removing use of guard helpers (and, not, or, etc.) from affected machines.

boneskull commented 3 months ago

Nevermind; I don't think it has to do with the type guard helpers. The workaround doesn't always work. 😄

I'll open a new issue if I figure out what's going on.

boneskull commented 3 months ago

I can confirm it starts being a problem at a0e9ebcef26552659ac6c2c785c138387eafc766

boneskull commented 3 months ago

Ref: #5048