Closed geirsagberg closed 2 weeks ago
The code works as expected; property c
exists, only the typing does not match. I know I can workaround with get() as unknown as ComputedState
but would be nice if this was not necessary.
The best way would be to make the root create
call (which determines the get()
function's typing) be templated with some FullState
type, perhaps:
type FullState = State & ReturnType<typeof computeState>
With type FullState = State & ReturnType<typeof computeState>
I get errors because create<FullState>
expects me to return values for the computed state.
Closest workaround I could find was:
type FullState = State & Partial<ReturnType<typeof computeState>>
Downside of this is that all computed values will be potentially undefined
when used in the create
call.
Hey, @geirsagberg, sorry to take so long to get back to you. Unfortunately, with Immer, Immer derives the type for it's SetState
from GetState
, and the ComputedStore
is explicitly left out of SetState
for now. Without Immer, get()
has the correct type in version 1.4.0.
One other suggestion, you could try swapping Immer & computed. Otherwise, closing this issue.
Hey, figured out the issue. Middleware types were not cascading down appropriately. I would suggest trying out version 2.0 and see if the new pattern brings the types through appropriately. I also opened (#2696)[https://github.com/pmndrs/zustand/pull/2696] to address the aforementioned issue with Immer deriving the setState
type from getState
. Let me know if that works.
Given the following code:
Is there a way to make
get()
automatically include the computed properties?