'Cannot apply patch, path doesn't resolve' when modifying property of an object in Map
🐛 Bug Report
applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b when applying inversePatches generated by the following produce call:
interface Test {
a: number;
b: string;
}
let state = {
map: new Map<number, Test>([])
};
state.map.set(1, { a: 5, b: "hello" }); // Setting initial state
let inversePatches: Patch[] = [];
state = produce(
state,
(draftState) => {
const test = draftState.map.get(1);
if (test) test.b = "world";
},
(_patches, _inversePatches) => {
inversePatches = _inversePatches;
}
);
It works fine when the patch op is add instead of replace:
state = produce(
state,
(draftState) => {
const test = draftState.map.get(1);
if (test) draftState.map.set(1, {...test, b: "world"}); // works
},
(_patches, _inversePatches) => {
inversePatches = _inversePatches;
}
);
Link to repro
https://codesandbox.io/s/awesome-stitch-xxtxq3?file=/src/index.ts
- Click
Set Map Propertybutton to set map property - Click
Undobutton to callapplyPatcheswith inversePatches
To Reproduce
Steps to reproduce the behavior:
- Call
enableMapSetandenablePatches - Create a
Mapwith an object as the value type and add some key-value pairs to it - Use
produceto mutate the Map - Inside
produce, retrieve an element usingMap.get()and modify a property on the element - Save the
inversePatchesgenerated byproduce - Call
applyPatches(map, inversePatches)
Observed behavior
applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b.
Expected behavior
The inversePatches should be applied successfully and state should revert back to the state it was in before the produce call
Environment
- Immer version: 9.0.14
- [x] I filed this report against the latest version of Immer
- [x] Occurs with
setUseProxies(true) - [x] Occurs with
setUseProxies(false)(ES5 only)
Getting the same error. Any updates on this?
:tada: This issue has been resolved in version 9.0.20 :tada:
The release is available on:
Your semantic-release bot :package::rocket: