statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.21k stars 1.26k forks source link

add `EventFromStore` utility type to `@xstate/store` #5020

Closed with-heart closed 3 months ago

with-heart commented 3 months ago

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.

changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: ffc19735c0f395a2c0ec5a2e81c27b798af35398

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