Infer Immer middleware type for setState
from setState
instead of getState
#1621
-
The Immer middleware infers the type nextStateOrUpdater for setState from getState: type StoreImmer<S> = S extends {
getState: () => infer T;
setState: infer SetState;
} ? SetState extends (...a: infer A) => infer Sr ? {
setState(nextStateOrUpdater: T | Partial<T> | ((state: Draft<T>) => void), shouldReplace?: boolean | undefined, ...a: SkipTwo<A>): Sr;
} : never : never; This is typically not a problem unless you're using a middleware like chrisvander/zustand-computed, where the state returned by getState differs from what is allowed to be set via setState. In the case of the computed middleware, getState returns additional keys beyond what the user is allowed to modify. Can we change the Immer state to infer off of the SetState function? E.g. use something like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
|
Beta Was this translation helpful? Give feedback.
https://tsplay.dev/WGgq9W
Now, I know why.
ComputedStore
has to be inferred before passing to the next middleware.