immer icon indicating copy to clipboard operation
immer copied to clipboard

[TS] IProduce (external-types.d.ts) curried overloading incorrect -> typecheck pass, runtime fail

Open akomm opened this issue 3 years ago • 2 comments

🐛 Bug Report

There is at least one cases of curried produce invocation with provided types, that pass type-check, but fail at runtime. The returned curried function incorrectly accepts undefined as State type-wise, but at runtime the recipe code will fail, when undefined is passed as state. This is because there are two overloading, of which one will always win due to the variance in the context. However, even the second overload which is wrongly picked does not make sense, because it suggests you get initialState as a second argument, which is either impossible or not documented. Both issues are demonstrated in the codesandbox and further explained.

I'v wrote explanation side-by-side with code in Sandbox.

Link to repro

Codesandbox I'v wrote explanation side-by-side with code.

To Reproduce

Open the Codesandbox and check open the codesandbox's console (tab underneath the preview)

Observed behavior

Curried function returned from producedoes accepts undefined as state.

Expected behavior

Curried function returned from producedoes not accept undefined as state.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version: 9.0.7
  • [X] I filed this report against the latest version of Immer
  • [X] Occurs with setUseProxies(true)
  • [X] Occurs with setUseProxies(false) (ES5 only)

akomm avatar Dec 03 '21 17:12 akomm

Interested in attempting a PR?

mweststrate avatar Jan 11 '22 13:01 mweststrate

@mweststrate I would. I had not time to figure out what scenario the overloading A (as commented in the codesandbox) is referring to. Did not find any match in the docs. If empty state is not allowed in any case, then the fix would be just remove the overloading. But if it has some use-case, obviously removing is no-opt

akomm avatar Jan 13 '22 08:01 akomm