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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript: programmatically updating multiple object fields in produce causes issues with index signature #1002
Comments
|
The |
This is the way TypeScript is. So when your code goes like this: /*...*/ partial: Partial<TState> /* ... */
for (let key in partial) {
/* ... */ TypeScript is not having any particular So when you later do: const value = partial[key]; value becomes This is because TypeScript is not analysing the code to the level to be able to guarantee that And there is the issue you are facing: you cannot assign This is a very valid case for using If you don't like const value = partial[key] ?? (()=>{throw new Error('Logic error, impossible situation, partial cannot have undefined under any key of partial.')})(); At least this way you know that you have a runtime guarantee as well. So if someone makes some changes to the code, and |
Thank you @BenceSzalai for the thorough explanation :) It seems that this cannot be resolved from the Immer side, so I'll close the issue 馃憤 |
馃檵鈥嶁檪 Question
I am unable to generate a correct type signature for a setter function that programmatically updates multiple fields in an Object Draft.
I am looking for advice on how to combine TypeScript and Immer to create a correct signature (hopefully without using casting
as
).Link to repro
I created a TypeScript playground where you can see the error reproduced.
Environment
"typescript": "~4.8.2"
"immer": "^9.0.16",
setUseProxies(true)
setUseProxies(false)
(ES5 only)The text was updated successfully, but these errors were encountered: