immerjs / immer

Create the next immutable state by mutating the current one
https://immerjs.github.io/immer/
MIT License
27.65k stars 848 forks source link

Unable to pass generic to produce for dynamic typing #1130

Open yasinitskyi opened 4 months ago

yasinitskyi commented 4 months ago

🙋‍♂ Question

I am unable to pass generic parameter type to my produce function in order to propagate that generic to type of draft itself like so: SearchContextProvider: Screenshot 2024-06-19 at 10 59 49 PM Reducer: Screenshot 2024-06-19 at 11 00 43 PM State for the draft to be applied: Screenshot 2024-06-19 at 11 01 21 PM Error: Screenshot 2024-06-19 at 11 01 42 PM

This only occures with generic, if I use concrete type I won't get such error (namely If I replace generic with ResultDummy - error vanishes.

How do I go about it since this Provider supposed to be used in different part of the code and should receive different type depends on part of the code where it is used. Thank you!

Here is playground where I reproduced the issue: https://codesandbox.io/s/produce-with-generic-vchkj4

Environment

We only accept questions against the latest Immer version.