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

add EventFromStore utility type to @xstate/store #5020

Merged
merged 2 commits into from
Aug 5, 2024

Conversation

with-heart
Copy link
Contributor

This PR adds the EventFromStore utility type to @xstate/store. EventFromStore allows us to extract the type of events from a given store:

import { createStore, type EventFromStore } from '@xstate/store';

const store = createStore(
  { count: 0 },
  {
    add: (context, event: { addend: number }) => ({
      count: context.count + event.addend
    }),
    multiply: (context, event: { multiplier: number }) => ({
      count: context.count * event.multiplier
    })
  }
);

type StoreEvent = EventFromStore<typeof store>;
//   ^? { type: 'add'; addend: number } | { type: 'multiply'; multiplier: number }

This idea came about thanks to @qiushiyan in #5017.

Copy link

changeset-bot bot commented Aug 5, 2024

🦋 Changeset detected

Latest commit: ffc1973

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

This PR includes changesets to release 1 package
Name Type
@xstate/store Minor

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

Co-authored-by: David Khourshid <davidkpiano@gmail.com>
@davidkpiano davidkpiano merged commit e974797 into statelyai:main Aug 5, 2024
1 check passed
@github-actions github-actions bot mentioned this pull request Aug 5, 2024
@with-heart with-heart deleted the event-from-store branch August 5, 2024 20:34
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.

2 participants