Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Type issue: Actor<AnyStateMachine> not assignable to Actor<typeof machine> #5165

Open
Sartonon opened this issue Jan 2, 2025 · 1 comment
Labels

Comments

@Sartonon
Copy link

Sartonon commented Jan 2, 2025

XState version

XState version 5

Description

After updating xstate to the latest version we started having problems with with machine typing. We are using Actor<AnyStateMachine> but it seems to no longer work with Actor<typeof machine>

Here is the example code:

import { Actor, AnyStateMachine, createActor, setup } from 'xstate';

const feedbackMachine = setup({
  types: {
    context: { feedback: 'feedback' },
    events: {},
  } as {
    context: { feedback: string };
    events: { type: 'feedback.good' } | { type: 'feedback.bad' };
  },
  actions: {
    logTelemetry: () => {},
  },
}).createMachine({
  context: {
    feedback: 'hello',
  },
});

const a: Actor<typeof feedbackMachine> = createActor(
  feedbackMachine
) as Actor<AnyStateMachine>;

Actor<AnyStateMachine> should be assignable to Actor<typeof machine> but there seems to be the following error:

Property 'context' is optional in type 'Omit<StateNodeConfig<any, any, any, any, any, any, any, any, any, any>, "output"> & { version?: string | undefined; output?: any; } & { context?: any; } & { ...; }' but required in type '{ context: InitialContext<{ feedback: string; }, any, any, { type: "feedback.good"; } | { type: "feedback.bad"; }>; }'

This is not how we use it in our codebase but it was the easies way to reproduce the same error.

Expected result

Actor<AnyStateMachine> should be assignable to Actor<typeof machine>?

Actual result

Type error:

Property 'context' is optional in type 'Omit<StateNodeConfig<any, any, any, any, any, any, any, any, any, any>, "output"> & { version?: string | undefined; output?: any; } & { context?: any; } & { ...; }' but required in type '{ context: InitialContext<{ feedback: string; }, any, any, { type: "feedback.good"; } | { type: "feedback.bad"; }>; }'

Reproduction

https://stackblitz.com/edit/vitejs-vite-mrgs8ubw?file=src%2Fmain.ts

Additional context

No response

@Sartonon Sartonon added the bug label Jan 2, 2025
@Sartonon
Copy link
Author

Sartonon commented Jan 3, 2025

We were able to resolve this issue by using ActorRefFrom<typeof machine> and AnyActorRef instead of Actor<typeof machine>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant