Skip to content

Bug: type error when using stateIn guard with setup() #5358

@aris-lvm

Description

@aris-lvm

XState version

XState version 5

Description

There's a TS error when using stateIn guard within a setup(...).createMachine(...) definition. With just createMachine(...) the error goes away.

➜  xstate-state-in-issue git:(main) ✗ npx tsc --noEmit
index.ts:26:9 - error TS2322: Type '{ guard: GuardPredicate<MachineContext, { type: "TOGGLE"; }, undefined, any>; }' is not assignable to type 'TransitionConfigOrTarget<MachineContext, { type: "TOGGLE"; }, { type: "TOGGLE"; }, never, never, never, never, EventObject, MetaObject>'.
  Types of property 'guard' are incompatible.
    Type 'GuardPredicate<MachineContext, { type: "TOGGLE"; }, undefined, any>' is not assignable to type 'GuardPredicate<MachineContext, { type: "TOGGLE"; }, undefined, never>'.
      Type 'any' is not assignable to type 'never'.

26         TOGGLE: { guard: stateIn("idle") },
           ~~~~~~


Found 1 error in index.ts:26

Here's the machine definition:

export const exampleMachineWithSetup = setup({
  types: {
    events: {} as { type: "TOGGLE" },
  },
}).createMachine({
  initial: "idle",
  states: {
    idle: {
      on: {
        TOGGLE: { guard: stateIn("idle") },
      },
    },
  },
});

Expected result

There should be no error, as with the standalone createMachine(...) definition.

Actual result

See the error message above.

Reproduction

https://github.com/aris-lvm/xstate-state-in-issue

Additional context

XState version: 5.20.2
TypeScript version: 5.9.2

Sorry for providing the repo instead of one of the templates. On CodeSandbox there was a completely unrelated TS error and StackBlitz confused me with it's AI.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions