immerjs / immerjs/immer

Unexpected undefined not assigned

Open
#1,160 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

🐛 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

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 by running the TypeScript reproduction with produceWithPatches and inspect the implementation paths it exercises for assignments inherited from a prototype. Compare the generated patches and own-property behavior with the expected output; done means assigning undefined creates an own property and an add patch without changing the original object.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.