WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Unexpected undefined not assigned #1160

@Lin-coln

Description

@Lin-coln

🐛 Bug Report

When I assign "undefined" to the prop "foo" of draft, and the prototype of base has the prop "foo" which is "undefined",
the final return value is not assigned an "undefined" to the prop "foo".

To Reproduce

import { enablePatches, immerable, produceWithPatches } from "immer";

const proto = { [immerable]: true, name: undefined };
const foo = Object.create(proto);

console.log(`[hasOwnProp] foo:`, Object.prototype.hasOwnProperty.call(foo, "name"));

enablePatches();
const [foo_next, patches, _] = produceWithPatches(foo, (x) => {
  console.log(`[immer] produce foo_next from immer`);
  x.name = undefined;
});

console.log(`[immer] foo_next patches:`, patches);

console.log(`[hasOwnProp] foo:`, Object.prototype.hasOwnProperty.call(foo, "name"));
console.log(`[hasOwnProp] foo_next:`, Object.prototype.hasOwnProperty.call(foo_next, "name"));

foo.name = undefined;
console.log(`[vanilla] assign name manually`);
console.log(`[hasOwnProp] foo:`, Object.prototype.hasOwnProperty.call(foo, "name"));

Observed behavior

[hasOwnProp] foo: false
[immer] produce foo_next from immer
[immer] foo_next patches: []
[hasOwnProp] foo: false
[hasOwnProp] foo_next: false
[vanilla] assign name manually
[hasOwnProp] foo: true

Expected behavior

[hasOwnProp] foo: false
[immer] produce foo_next from immer
[immer] foo_next patches: [
  {
    op: "add",
    path: [ "name" ],
    value: undefined,
  }
]
[hasOwnProp] foo: false
[hasOwnProp] foo_next: true
[vanilla] assign name manually
[hasOwnProp] foo: true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions