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

Example from the docs not working #109

Open
a-rebets opened this issue Feb 28, 2025 · 0 comments
Open

Example from the docs not working #109

a-rebets opened this issue Feb 28, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@a-rebets
Copy link

Description

I tried to onboard to this library after using the standard immer / zustand combo for quite some time. Apparently adapting my store was not straightforward. Maybe I'm just missing the correct way we should be dealing with mutative-like updates, but docs don't say much. I tried changing my types or playing with store init options but failed to find a fix. I then started trying examples from the docs and ended up with a type error even on simple stuff
bug

Steps

  1. Install zustand-x/zustand in a clean TS project
  2. Paste the example from docs (comparing with Zustand section)
  3. Observe type error
const store = createStore(
    { count: 0 },
    { name: "test", mutative: true })
	.extendSelectors(({ get }) => ({
		// Computed values are auto-memoized
		double: () => get("count") * 2,
	}))
	.extendActions(({ set }) => ({
		increment: () => set("count", (count) => count + 1),
	}));

type error:

Overload 1 of 3, '(key: "state", value: { count: number; } | ((state: DraftedObject<Partial<{ count: number; }>>) => void)): void', gave the following error.
    Argument of type '"count"' is not assignable to parameter of type '"state"'.
  Overload 2 of 3, '(key: "count", value: number): void', gave the following error.
    Argument of type '(count: any) => any' is not assignable to parameter of type 'number'.

so the overload handling a specific field update only accepts a new value, but not a function that depends on the current state

Expectation

It's expected that the set method destructured inside extendActions is typed correctly, i.e. accepts both a function for mutating a draft of the selected field and a raw value

@a-rebets a-rebets added the bug Something isn't working label Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant