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

Required input for spawn when defined inside referenced actor #5139

Conversation

SandroMaglione
Copy link
Contributor

Similar to #5055, this PR makes input required when using spawn and the input type in the referenced actor is defined.

This prevents issues when forgetting to pass an input when using spawn (causing runtime errors).

const childMachine = createMachine({
  types: { input: {} as { value: number } }
});

const machine = createMachine({
  types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } },
  context: ({ spawn }) => ({
    ref: spawn(
      childMachine,
      // Input is now required!
      { input: { value: 42 } }
    )
  })
});

Copy link

changeset-bot bot commented Dec 9, 2024

🦋 Changeset detected

Latest commit: 9ea95dc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
xstate Patch
@xstate/graph Patch
@xstate/react Patch
@xstate/solid Patch
@xstate/svelte Patch
@xstate/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -123,5 +138,5 @@ export function createSpawner(
actorRef.start();
});
return actorRef;
};
}) as Spawner<any>;
Copy link
Contributor Author

@SandroMaglione SandroMaglione Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type errors without this bypass. I think this is expected given that typescript has not enough information to correctly type-check all possible configurations.

All tests are passing, so I think it shouldn’t be an issue (the update is mostly on the types).

@davidkpiano davidkpiano requested a review from Andarist December 9, 2024 22:00
@davidkpiano davidkpiano merged commit bf6119a into statelyai:main Dec 21, 2024
1 check passed
@github-actions github-actions bot mentioned this pull request Dec 21, 2024
import { ActorRefFrom, assign, createMachine } from '../src';

describe('spawn inside machine', () => {
it('input is required when defined in actor', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't really check that it's required here, u just pass an input as an argument but the same test would pass without the patch introduced by this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be inverted with a @ts-expect-error instead? Or is there another better way to test types?

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

Successfully merging this pull request may close these issues.

3 participants