pmndrs / zustand

🐻 Bear necessities for state management in React

Home Page:https://zustand-demo.pmnd.rs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typescript + Slices = trouble

buzzdead opened this issue · comments

Summary

Lots of errors trying to implement slices with the code from docs, i.e the slices pattern found here:
https://docs.pmnd.rs/zustand/guides/typescript

Link to reproduction

https://docs.pmnd.rs/zustand/guides/typescript

Check List

  • I've already opened a discussion before opening this issue, or already discussed in other media.

Please include a minimal reproduction.
https://docs.pmnd.rs/zustand/guides/typescript

Please check this if you're filing an issue regarding TypeScript.

  • I've read the typescript guide, in particular that create is to be used as create<T>()(...) and not create<T>(...).

Type 'BearSlice' does not satisfy the constraint 'StoreApi<BearSlice & FishSlice>'.
Type 'BearSlice' is missing the following properties from type 'StoreApi<BearSlice & FishSlice>': setState, getState, subscribe, destroyts(2344)
interface BearSlice
No quick fixes available

Type '{ bears: number; addBear: () => any; eatFish: () => any; }' is not assignable to type 'BearSlice & FishSlice'.
Type '{ bears: number; addBear: () => any; eatFish: () => any; }' is missing the following properties from type 'FishSlice': fishes, addFishts(2322)
vanilla.d.ts(33, 163): The expected type comes from the return type of this signature.
(property) BearSlice.addBear: () => void

Module '"zustand"' has no exported member 'create'. Did you mean to use 'import create from "zustand"' instead?ts(2614)
import create
No quick fixes available

@buzzdead btw, now there's a package called zustand-slices that helps you with that. Give it a try a let us know

That seemed to take away all the errors i had, just installing the package.

We know that typing slices is a bit tricky. BTW, we are working on revamping docs.

Worked like a charm after installing the package. Thanks for the lightning fast response.
You really should put in the documentation that the package is necessary though, didn't find any guides, help from any ai or stackoverflow\reddit thread to help me with this. Got pretty close to going back to another state management system.

Anyway, Ill continue with sustand.

Worked like a charm after installing the package. Thanks for the lightning fast response. You really should put in the documentation that the package is necessary though, didn't find any guides, help from any ai or stackoverflow\reddit thread to help me with this. Got pretty close to going back to another state management system.

Anyway, Ill continue with sustand.

It's not necessary but is an option now

Anyway, next time make sure you share a typescript playground link when you face typescript issues or try to format your code so would be easy to read