produce function return type is narrow type of base when using const assertions
juanigaray opened this issue Β· comments
π Bug Report
Consider an object with a field "myNum" with a number 7 assigned to it. Suppose this object is read-only and you use a const assertion on it. If you were to produce a different object from it, applying the '++' operator to myNum, you'd expect the produced object to have myNum of type number or 8. But it is still 7.
Link to repro
To Reproduce
Steps to reproduce the behavior:
Perform a const assertion over an object, and then produce a new object mutating some of its values.
Observed behavior
The produced type indicates that the value is the former
Expected behavior
The produced type should indicate that the value is the latter: the value observed at runtime.
Environment
We only accept bug reports against the latest Immer version.
- Immer version:
- I filed this report against the latest version of Immer
- Occurs with
setUseProxies(true)
- Occurs with
setUseProxies(false)
(ES5 only)
Yes, that would be a workaround for my specific case. To me, this is a weird behavior for a library I use. Is this the intended behavior?
I've done some digging and it seems to be possible to widen some input types in case it's not. See here. Maybe with a little tinkering I can manage to make produce return a Wide<Base>
instead of Base
. But I need to know if this is the intended behavior before I start working on a PR.
Maybe I worded this badly. I believe that the return type of a function should match the instances that it outputs. Hence the example where the value is 8
but the type is 7
.
I hadn't considered enums, they would break if the types were widened. In any case, if the expectation is that we should simply pass wider types to avoid incongruent outputs I won't look into it. Thanks for your replies.