Skip to content

Reassigning to the same Date object generates new copy with empty patches #858

Open
@somebody32

Description

@somebody32

🐛 Bug Report

If you mutate Date property inside produce but then revert it, immer still assumes changes while returning an empty set of patches.

Link to repro

https://codesandbox.io/s/immer-reassigning-breaks-immutabillity-2u7ob

import {enablePatches, produceWithPatches} from "immer"

enablePatches();

type DataObject = { 
  date: Date;
}

let initialDate = new Date();
let data: DataObject[] = [{date: initialDate}];

let [newData, patches] = produceWithPatches(data, draft => {
  let element = draft[0];
  let dateBefore = element.date;
 
  element.date = new Date()
  element.date = dateBefore;
});

console.log("Patches:", patches) // []
console.log("New Date equal to Initial", newData[0].date === initialDate) // true
console.log("Array equal", newData === data); // expect true but getting false

Environment

Immer version: 9.0.6

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions