Reassigning to the same Date object generates new copy with empty patches
Open
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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