statelyai / xstate

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

Bug: Type issue with `not` helper #5090

Open flugg opened 8 hours ago

flugg commented 8 hours ago

XState version

XState version 5

Description

I'm trying to create a package that creates a reusable state machine. I've simplified our machine to locate the error being the not function:

import { not, setup } from 'xstate';

export const myMachine = setup({
  guards: {
    myGuard: () => true,
  },
}).createMachine({
  states: {
    foo: {
      on: {
        someEvent: {
          target: 'bar',
          guard: not('myGuard'),
        },
      },
    },
    bar: {},
  },
});

Expected result

I expected the typings to be built without issues.

Actual result

When trying to build types for the packages, I get the following error:

src/my-machine.ts:3:14 - error TS2742: The inferred type of 'myMachine' cannot be named without a reference to '../../../node_modules/xstate/dist/declarations/src/guards'. This is likely not portable. A type annotation is necessary.

3 export const myMachine = setup({

It seems like the issue stems from the not function referencing the GuardPredicate type which is not exported by XState and TypeScript is unable to build a type for it without this reference.

Reproduction

https://codesandbox.io/p/devbox/angry-goldberg-v42j8m?workspaceId=4a3e8946-02ff-4af1-aeb6-deeb01d9e58a

Additional context

Try running pnpm run build in the reproduction and you will see the error. For some reason, the error doesn't occur when using...

"moduleResolution": "node",

...in tsconfig.json. But that doesn't seem like a viable solution as that causes other module issues and is not really recommended by TS at this point.

flugg commented 8 hours ago

I also found this issue which seems to have the same issue, but was closed without further investigation.

And as the reproduction above seems to prove, this doesn't seem to have anything to do with the size of the machines.