Skip to content

inconsistent behavior with undefined value #807

@tvolodimir

Description

@tvolodimir

🐛 Bug Report

Starting from 8.0.2, was changed 'produce' behavior with assigning 'undefined' value. And now it looks not consistent.

Link to repro

https://runkit.com/tvolodimir/immer-undefined-behaviour

To Reproduce

// const immer = require('[email protected]');
const immer = require('[email protected]');

const state = {};
const newState = immer.produce(state, draft => {
    draft.baz = undefined;
});

// output: 
// @9.0.2: false 
// @8.0.1: false
console.log(state === newState); 

// output: 
// @9.0.2: {}
// @8.0.1: {baz: undefined}
console.log(newState);  

Observed behavior

Behavior at 8.0.1 looks normal as I am changed state from {} to {baz: undefined} and of course, it's a new object.
But at 8.0.2-9.0.2 I see no change from state {} to {} but somehow get a new object, that is cause incomprehension and also cause problems at existing code

Expected behavior

First of all how to properly handle this in 8.0.2-9.0.2.

const newState = immer.produce(state, draft => {
  if (draft.baz !=== undefined) { // should I have to add checks before each assigning ?
      draft.baz = undefined;
  }
});

Or there are possibility to force produce to add fields with 'undefined' values?

Environment

  • Immer version:
  • I filed this report against the 9.0.2 version of Immer
  • Occurs with setUseProxies(true)

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