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: React Context Typescript not resolving children actors when selected #5181

Open
AllanFerencz-Lee opened this issue Jan 30, 2025 · 3 comments
Labels

Comments

@AllanFerencz-Lee
Copy link

XState version

XState version 5

Description

I'm new to XState and still learning to use it effectively. I'm unsure if I ran into a bug or if I'm using the library unintentionally.

Problem

I've been struggling for countless hours on this issue. Hopefully, someone has a solution.

My goal is to select context values and send events to a child machine actor of a root machine.

The core of my issue - Typescript doesn't narrow to the selected child actor. This happens once multiple actors are set up on the machine. When I console log the selected actor - It does have the correct value I'm looking for. I've included examples below.

Image

The Error

Property 'context' does not exist on type 'PromiseSnapshot<AuthUser, NonReducibleUnknown> | MachineSnapshot<{ count: number; }, { type: "inc"; } | { type: "dec"; }, {}, {}, string, NonReducibleUnknown, MetaObject, { ...; }>'.
  Property 'context' does not exist on type '{ status: "active"; output: undefined; error: undefined; } & { input: NonReducibleUnknown; }'.ts(2339)

Console logging the values outputs the expected actor
Image

I was able to reproduce the issue on code sandbox.

Navigate to App.tsx to see the comments.
https://codesandbox.io/p/devbox/busy-mopsa-n2crkj?workspaceId=ws_NiJ4KeBNbFceadwTQwz1Xf

The child machine is invoked in the initial state of the machine.

Is this a bug with XState or am I doing something wrong?

Expected result

Typescript narrowing should find the targeted actor.

Actual result

Typescript fails to narrow to the selected child actor.

Reproduction

https://codesandbox.io/p/devbox/busy-mopsa-n2crkj?workspaceId=ws_NiJ4KeBNbFceadwTQwz1Xf

Additional context

No response

@AllanFerencz-Lee AllanFerencz-Lee changed the title Bug: React Context Typescript not resolving children actors when selected. Bug: React Context Typescript not resolving children actors when selected Jan 30, 2025
@davidkpiano
Copy link
Member

Taking a quick look at this, it seems like you don't have the types for the actors set up.

See here: https://stately.ai/docs/actors#actors-and-typescript

@AllanFerencz-Lee
Copy link
Author

Thank you so much for your quick response. I really appreciate it.

I put the actors in the setup function like this.

Image

I've tried several different iterations but can't get it right.

What am I missing that I need to add?

@AllanFerencz-Lee
Copy link
Author

Do any examples exist that have something similar to what I'm trying to do?

  1. A Root machine that contains multiple actors
  2. One actor is anything that's not a machine
  3. One actor is another machine
  4. Select context data from the child machine
  5. In typescript

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

2 participants