Skip to content

fix: undefined assigned to a prototype-inherited key gets dropped#1262

Merged
mweststrate merged 1 commit into
immerjs:mainfrom
chatman-media:fix/prototype-undefined-not-stored-as-own-prop
Jul 3, 2026
Merged

fix: undefined assigned to a prototype-inherited key gets dropped#1262
mweststrate merged 1 commit into
immerjs:mainfrom
chatman-media:fix/prototype-undefined-not-stored-as-own-prop

Conversation

@chatman-media

Copy link
Copy Markdown
Contributor

Ran into this via #1160. If a draft's prototype has a key sitting at undefined (a class field with no initializer, or anything made with Object.create), assigning undefined to that same key on the draft is silently swallowed — no own property shows up on the result and no patch gets generated.

The set trap uses prop in state.copy_ to tell "already has this key" apart from "brand new key," but in walks the prototype chain, so an inherited key looks like it's already there even though it isn't an own property yet. Swapped it for the has() own-property check that's already used a few lines above for basically the same reason.

Added a test in regressions.js that checks both hasOwnProperty on the result and the patch that comes out of produceWithPatches.

If an object's prototype has a key set to undefined and you assign
undefined to that same key on a draft, immer treats it as a no-op
since `prop in state.copy_` walks the prototype chain and finds it
"already there." No own property gets created, no patch gets
recorded. Swapped that for the has() own-property check that's
already used a few lines up for the same purpose, and added a
regressions test.
@mweststrate

Copy link
Copy Markdown
Collaborator

Looking good, thanks for noticing and fixing!

@mweststrate mweststrate merged commit 16e225b into immerjs:main Jul 3, 2026
1 check passed
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 11.1.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants