zustand: subscribeWithSelector break types with Typescript

Hello,

I have issue with the new middleware subscribeWithSelector

Screenshot 2021-10-28 at 16 27 38

I dont’t understand why…

Previously I used ramda pipe function to combine my differents middlware in a more clean way, but the same error…

More generaly, Zustand lack of typescript examples / documentation, I’m often struggled with Type error using zustand

Could you help me ? Thanks !

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 21 (13 by maintainers)

Most upvoted comments

That said, before looking for better solution in general, I would like to find a specific solution to your issue.

How does this help?

create(devtools(subscribeWithSelector<IState>(persist(immer((set, get, api) => {

~Hm, maybe we should remove Omit<..., 'subscribe'>.~ No, it errors… Okay, if I remove deprecated signature, it can.

Hi, thanks for raising this.

Typing middleware is really hard and almost impossible to infer type nicely. I agree docs and examples are missing. Especially, combining mutiple middlewares with types requires some rules and patterns.

And, we are still seeking… So, appreciate for this example.

The rule of thumb (for now):

  1. order devtools, subscribeWithSelector, persist, and immer.
  2. add State type near the leaf.

(Typing root create is trickier.)

Hope it helps.