Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing of get() does not include computed state #15

Closed
geirsagberg opened this issue Dec 30, 2023 · 6 comments
Closed

Typing of get() does not include computed state #15

geirsagberg opened this issue Dec 30, 2023 · 6 comments

Comments

@geirsagberg
Copy link

Given the following code:

interface State {
  a: number
  b: number
}

function computeState({ a, b }: State) {
  return {
    c: a + b,
  }
}

const useState = create<State>()(
  computed(
    immer((set, get) => ({
      a: 1,
      b: 2,
      incrementAndLog: () => {
        set((state) => {
          state.a += 1
          state.b += 1
        })
        // Error: Property 'c' does not exist on type 'State'
        const { c } = get()
        console.log(c)
      },
    })),
    computeState
  )
)

Is there a way to make get() automatically include the computed properties?

@geirsagberg
Copy link
Author

geirsagberg commented Dec 30, 2023

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.

@chrisvander
Copy link
Owner

chrisvander commented Jan 1, 2024

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>

@geirsagberg
Copy link
Author

geirsagberg commented Jan 14, 2024

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.

@chrisvander
Copy link
Owner

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.

@chrisvander
Copy link
Owner

One other suggestion, you could try swapping Immer & computed. Otherwise, closing this issue.

@chrisvander
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants