Version 9 regression with readonly arrays
Zamiell opened this issue Β· comments
π Bug Report
When upgrading from immer 8 to immer 9, I get new errors in my codebase from the TypeScript compiler. These errors occur when I assign a new object on a draft state, and that new object contains a substate that has one or more properties that are a readonly array.
Link to repro
Here is a basic minimal reproduction of the error. I'm trying to update the substate of a state, and I get this error.
The error can be removed by changing: readonly someStrings: readonly string[];
--> readonly someStrings: string[];
However, this seems bad - the whole point of using immer in the first place is that you get to have immutable state + immutable data structures. So I'm not sure this "fix" makes sense.
Is this a regression? Or is there some new way that I am supposed to structure my code in immer version 9?
Environment
- Immer version:
- I filed this report against the latest version of Immer
- Occurs with
setUseProxies(true)
- Occurs with
setUseProxies(false)
(ES5 only)
Your new substate is readonly, so cannot be assigned to something that is (temporarily) mutable. You can use castDraft: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgESgQwGYwDRwMZoDOMqm8AvnBlBCHAESggCmU9A3AFCfAB2MrDGjzM4AWQCeAZRhoBiTnDhRmaACYReAGwlwQ0gK4AjGXOYAucYZOyBXctz4CoQkVanHT8hIuWqN2rqEtMwyUHwA5pbE4bwRXEoq6po6cMEsYZGElkkBqTGRANoAuvbcGAa8eDDAmnARzCTMQgZaMJIeNmYAFACUlh2etqI+iY0GULwKSkrpoTCxUQwYEBD02L6zIZlx2XCF9CtruPRGaGzFG0oODpwVVTV1+gBKzGoGIlAAYpXVtbzdYhmSykLAAHg6wwAfL1pvhNMQ9NYvKIALz1RrIZpoVrtZHDPoJNLDAB0+k6KLg6IIxFBMG65KGZl6ZSAA
Thank you!