immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Immer with map

doneumark opened this issue · comments

I use immer with typescript, and I have a state that looks like this:
State { map: Map<string, { id: string } | { key: string }>

When I try to set a value in the map, like this:

produce<State>((state) => {
    const { map } = state;
    map.set('some-key', { id: '123' });
}),

I get a typescript error:
Argument of type '{ id: string }' is not assignable to parameter of type 'WritableDraft<{ id: string } | { key: string }>'.

Did anyone encountered this before?

** immer v: "^9.0.21",
*** I used the enableMapSet() before this code.

@mweststrate thanks, I updated the code in the comment, it was actually map instead of keys. same problem though.

I read the casting guide but unfortunately- castDraft did not work

Closing for lack of repro / not following issue template