zustand: Persist Partialize not working with TypeScript
I’ve been having trouble getting Zustand’s persist partialize middleware, to work with TypeScript. I’m not sure what type to provide and where.
Thanks in advance!
Here is the TS error I am currently getting.
Argument of type 'StateCreator<Store, [], [["zustand/persist", { siteId: string; }]], Store>'
is not assignable to parameter of type' StateCreator<Store, [], [["zustand/persist", Store]], Store>'.
Type 'StateCreator<Store, [], [["zustand/persist", { siteId: string; }]], Store>'
is not assignable to type '{ $$storeMutators?: [["zustand/persist", Store]]; }'.
Types of property '$$storeMutators' are incompatible.
Type '[["zustand/persist", { siteId: string; }]]' is not assignable to type '[["zustand/persist", Store]]'.
Type '["zustand/persist", { siteId: string; }]' is not assignable to type '["zustand/persist", Store]'.
Type at position 1 in source is not compatible with type at position 1 in target.
Type '{ siteId: string; }' is missing the following properties from type 'Store': tooltip, setTooltip, email, setEmail, and 5 more.
Here is my store in a codesandbox: https://codesandbox.io/s/flamboyant-sun-mwhr13?file=/store.ts
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (1 by maintainers)
Hey @Fitzpa,
you are actually only missing one pair of
()
behind your call tocreate
(your codeconst useStore = create<Store>(..)
), so doingconst useStore = create<Store>()(..)
will work. You can also see that in the typescript example in the readme https://github.com/pmndrs/zustand#typescript-usageBest
Wow! I looked at that example so many times and totally missed that parens every time. Haha. Yesterday I got this to work too. Though I’m not sure which is better. Your/the examples way seems way easier. Thank you so much!
I tried using the same with the latest zustand 4.4.5, but running into the same error.
Typescript version is 5.0.2.
Here is the code:
The error:
See also #2163 We are working on it. Please use the previous version meanwhile. Sorry for the inconvenience.
I also found this article to be helpful where you create a persist type: https://cloverinks.medium.com/nextjs-zustand-typescript-type-conflict-with-persist-middleware-d72c2b3ec286
try to use v4.1.0, it is fine
have a try with v4.1.0
hi @dai-shi, can you help with this. im facing this error too. its happening while using persist and also immer for me.