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

[@xstate/store] Fix createStoreWithProducer(…) types #4890

Merged
merged 3 commits into from
May 12, 2024

Conversation

davidkpiano
Copy link
Member

The context type for createStoreWithProducer(producer, context, transitions) will now be properly inferred.

const store = createStoreWithProducer(
  produce,
  {
    count: 0
  },
  {
    // ...
  }
);

store.getSnapshot().context;
// BEFORE: StoreContext
// NOW: { count: number }

Copy link

changeset-bot bot commented May 11, 2024

🦋 Changeset detected

Latest commit: 30bebf5

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 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

}
);

store.getSnapshot().context satisfies { count: number };
Copy link
Member

Choose a reason for hiding this comment

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

minimal repro case (TS playground):

type StoreContext = Record<string, any>;

interface IProduce {
  <State, Recipe extends Function>(recipe: Recipe, initialState: State): State;
  <Base>(base: Base, recipe: (draft: Base) => void): Base;
}

declare const produce: IProduce;

declare function createStoreWithProducer<TContext extends StoreContext>(
  producer: (
    context: TContext,
    recipe: (context: TContext) => void,
  ) => TContext,
  initialContext: TContext,
): {
  getSnapshot: () => { context: TContext };
};

const store = createStoreWithProducer(produce, {
  count: 0,
});

store.getSnapshot().context satisfies { count: number };

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
@davidkpiano davidkpiano merged commit 6d92b77 into main May 12, 2024
1 check passed
@davidkpiano davidkpiano deleted the davidkpiano/fix-store-producer-types branch May 12, 2024 22:59
@github-actions github-actions bot mentioned this pull request May 12, 2024
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

2 participants