diff --git a/src/object/assign.ts b/src/object/assign.ts index 22138332..d46f3367 100644 --- a/src/object/assign.ts +++ b/src/object/assign.ts @@ -25,12 +25,11 @@ export function assign>( const merged = proto ? { ...initial } : Object.assign(Object.create(proto), initial) - for (const key in override) { - if (Object.prototype.hasOwnProperty.call(override, key)) { - merged[key] = isPlainObject(initial[key]) + for (const key of Object.keys(override)) { + merged[key] = + isPlainObject(initial[key]) && isPlainObject(override[key]) ? assign(initial[key], override[key]) : override[key] - } } return merged } diff --git a/tests/object/assign.test.ts b/tests/object/assign.test.ts index 78bdfc51..75b23591 100644 --- a/tests/object/assign.test.ts +++ b/tests/object/assign.test.ts @@ -49,6 +49,10 @@ describe('assign', () => { const result = _.assign({}, { b: 'y' }) expect(result).toEqual({ b: 'y' }) }) + test('handles null overriding nested object', () => { + const result = _.assign({ a: { b: { c: 1 } } }, { a: { b: null } }) + expect(result).toEqual({ a: { b: null } }) + }) test('works with Object.create(null)', () => { const object = { a: Object.create(null) } object.a.b = 1