-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Immer bans assignment to the nested parts of state #4830
Comments
I don't think this is an Immer issue per se, so much as it's a use of the generics and wrapper function around You can wrap |
Well, i understood the reason of that from the typescript's point of view. The question is how to avoid that, using RTK. This typing issue brought by Immer, and i can't even write this reducer in immutable manner.
Oh, that requirements do not make a lot of sense, either. I tried to specify state type explicitly in the reducer before raising this issue. Using
I mention this workaround to only demonstrate how ugly and invasive Immer is, considering reducers, but of course, The super-power of RTK, which i personally love it for, is how it elegantly binds actions and reducers together, allowing create generic things. Immer, in the context of RTK, is a grass-cutter hard-plugged to a sport car. You still can use it for trivial purposes, but you'd better not try to speed it up. Sorry, i was too emotional and thank you for this brilliant library. |
@MostFrumiousBandersnatch our stance on Immer hasn't changed. It's a core part of RTK, and it's the right choice for our users. I gave an extensive re-answer on this recently: Per that comment, we've experimented with trying to make use of Immer configurable, and it's too complex and not on our roadmap atm.
The types work fine out of the box. There _are _edge cases, and you're running into one of them, specific to dealing with wrapping and use of generics. Per that issue, yes Immer has a perf impact, but most of the time it's not a problem. Immer still simplifies reducers and prevents accidental mutations. Overall, Immer does work great in RTK, and we're not changing it. |
Hello!
Recently faced with some strange behavior of reducers, trying to create a generic slice creator:
(stack-blitz)
in discard reducer i got:
That's fixable with workaround like
But i supposed immer to be an invisible companion of redux-toolkit, and it looks weird anyway.
Similar code outside (written in immutable manner) of reducer causes not problems with type-checker.
Could you please explain what I've done wrong?
Thank you in advance!
The text was updated successfully, but these errors were encountered: