immerjs / immerjs/immer

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

Open
#858 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

complexity-medium PR welcome
Dominant language
JavaScript
Stars
29k
Forks
881
PR merge metrics
No merged PRs in 30d

Description

🐛 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)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox reproduction and the produceWithPatches entry point, checking the behavior under both Proxy and ES5 modes described in the report. The fix is complete when reverting a Date assignment returns the original array identity while still producing an empty patch set in both environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.