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

fix: use event generic in ServiceConfig type #1372

Merged
merged 2 commits into from Aug 11, 2020

Conversation

christianchown
Copy link

Hi. Ran into a typing problem when using MachineConfig. The following code does not compile in TypeScript with "strict": true

type TestContext = {};
type TestEvent =
  | { type: 'EVENT_WITH_PAYLOAD'; payload: string }
  | { type: 'OTHER_EVENT_WITH_PAYLOAD'; payload: number };

const services = {
  testService: (_context: TestContext, _event: TestEvent) => Promise.resolve(),
  testService2: (_context: TestContext, _event: TestEvent) => Promise.resolve(),
};

const partialMachineOptions: Partial<MachineOptions<TestContext, TestEvent>> = {
  services, 
  // Type ... is not assignable to type 'Record<string, ServiceConfig<TestContext, AnyEventObject>>'
  // ...
  // Types of parameters '_event' and 'event' are incompatible. 
  // Type 'AnyEventObject' is not assignable to type 'TestEvent'.
};

This is because ServiceConfig expects an InvokeCreator that uses AnyEventObject.

In TypeScript's strict mode, TestEvent is not compatible with AnyEventObject as all TestEvents are inferred as having a payload property.

This PR fixes the problem by having ServiceConfig (like the other types in MachineOptions) use the supplied event generic.

@changeset-bot
Copy link

changeset-bot bot commented Aug 11, 2020

🦋 Changeset is good to go

Latest commit: 8270d5a

We got this.

This PR includes changesets to release 1 package
Name Type
xstate 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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 11, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 8270d5a:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

@davidkpiano
Copy link
Member

Thanks! Would you mind adding a quick changeset?

@christianchown
Copy link
Author

thanks for reviewing so swiftly @davidkpiano, changeset added!

@davidkpiano davidkpiano merged commit 39abda6 into statelyai:master Aug 11, 2020
@github-actions github-actions bot mentioned this pull request Aug 11, 2020
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.

None yet

3 participants