zustand: zustand 4.1.2 create() initializer type problems with store mutators
I have a function that extracts out the initializer for a store to a function so that I can create many variants of a particular store, in this case various cache stores.
I was previously working with zustand 4.1.1 and the only type errors that I had were the getter and setter possibly being undefined, so I could suppress them using assertions.
After updating to zustand 4.1.2, there are now new type errors that prevent building, and I cannot for the life of me figure out what’s going on with them.
I made a minimal CodeSandbox demo of the problem available here.
A possibly notable observation is that without the M
parameter, the type of set
is reported to be (partial: Cache<K, V> | Partial<Cache<K, V>> | ((state: Cache<K, V>) => Cache<K, V> | Partial<Cache<K, V>>), replace?: boolean | undefined) => void
.
With the M
parameter, it instead becomes Get<Mutate<StoreApi<Cache<K, V>>, M>, "setState", undefined>
.
The closest I’ve gotten to figuring out a possible cause is that the Get
type might not be inferring that the "setState"
and "getState"
fields should (I think) be guaranteed to be present, but that’s really just grasping at straws.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (12 by maintainers)
You want create()() for TS:
You need to use
persist
for vanilla store.btw, I’m planning to deprecate
create(vanillaStore)
usage in the future. Because unlike v3, it’s easy to do in userland with v4 api.Yeah, I kinda agree. The good part is there are still no restrictions, users can use middlewares that don’t preserve the subtype relationship and they’d still seamlessly work, it’s just that we’re making a little assumption to improve types of
StateCreator
when the mutator is generic.Oops, I made mistakes in the descriptions of the PRs haha. #1371 is option 2 and #1372 is option 1.