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

Improve the type of callback invoke params #2125

Merged
merged 1 commit into from
Apr 22, 2021

Conversation

VanTanev
Copy link
Contributor

@VanTanev VanTanev commented Apr 21, 2021

type Event = { type: 'START' } | { type: 'STOP' }

const machine = createMachine<{}, Event>({
  invoke: {
    id: 'callback-service',
    src: () => (callback, onReceive) => {
      onReceive(event => {
        // event is { type: 'START' } | { type: 'STOP' }
      })
      callback({ type: 'I_DONT_EXIST' }) // type errors
    }
  }
})

@changeset-bot
Copy link

changeset-bot bot commented Apr 21, 2021

🦋 Changeset detected

Latest commit: a61d01c

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

This PR includes changesets to release 3 packages
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

@VanTanev VanTanev force-pushed the improve-invoke-callback-type branch from 110ec49 to a61d01c Compare April 21, 2021 12:18
@VanTanev
Copy link
Contributor Author

@Andarist @mattpocock this should be a pretty straightforward type improvement. It didn't break any of my projects, but it would be nice if you could test with yours too.

Copy link
Member

@davidkpiano davidkpiano left a comment

Choose a reason for hiding this comment

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

This makes sense to me. I think an initial motivation for having it as any was that in some situations, it's not easy for the child to know what event types it could send the parent. But there's ways around that.

@mattpocock
Copy link
Contributor

@davidkpiano I 100% agree with @VanTanev that this should be typed as a Sender<TEvent>. I really like using the invoked callback pattern but having to manually type this is not particularly fun. I also haven't found a case where I want it typed as anything else.

@davidkpiano davidkpiano merged commit e774852 into statelyai:master Apr 22, 2021
This was referenced Apr 22, 2021
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