Skip to content

Commit dc3f66c

Browse files
committed
fix: #807 new undefined properties should end up in result object
1 parent 5412c9f commit dc3f66c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

__tests__/regressions.js

+12
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,17 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
239239
bar: {x: 3}
240240
})
241241
})
242+
243+
test("#807 new undefined member not stored", () => {
244+
const state = {}
245+
const newState = produce(state, draft => {
246+
draft.baz = undefined
247+
})
248+
expect(state).not.toBe(newState)
249+
expect(Object.hasOwnProperty.call(newState, "baz")).toBe(true)
250+
expect(newState).toEqual({
251+
baz: undefined
252+
})
253+
})
242254
})
243255
}

src/core/proxy.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ export const objectTraps: ProxyHandler<ProxyState> = {
157157
markChanged(state)
158158
}
159159

160-
if (state.copy_![prop] === value && typeof value !== "number") return true
160+
if (
161+
state.copy_![prop] === value &&
162+
// special case: NaN
163+
typeof value !== "number" &&
164+
// special case: handle new props with value 'undefined'
165+
(value !== undefined || prop in state.copy_)
166+
)
167+
return true
161168

162169
// @ts-ignore
163170
state.copy_![prop] = value

0 commit comments

Comments
 (0)