diff --git a/__tests__/updateIn.ts b/__tests__/updateIn.ts index e9a1a6691c..b3104832fe 100644 --- a/__tests__/updateIn.ts +++ b/__tests__/updateIn.ts @@ -87,6 +87,20 @@ describe('updateIn', () => { }); }); + it('deep set with numeric key 1', () => { + const m = fromJS({ a: { b: 1 } }); + expect(m.updateIn(['a', 'c', 0], () => 20).toJS()).toEqual({ + a: { b: 1, c: [20] }, + }); + }); + + it('deep set with numeric key 2', () => { + const m = fromJS({}); + expect(m.updateIn(['a', 0], () => 20).toJS()).toEqual({ + a: [20], + }); + }); + it('deep push', () => { const m = fromJS({ a: { b: [1, 2, 3] } }); expect(m.updateIn(['a', 'b'], list => list.push(4)).toJS()).toEqual({ diff --git a/src/functional/updateIn.js b/src/functional/updateIn.js index 695386ae00..65e2582cf7 100644 --- a/src/functional/updateIn.js +++ b/src/functional/updateIn.js @@ -11,6 +11,7 @@ import isDataStructure from '../utils/isDataStructure'; import quoteString from '../utils/quoteString'; import { NOT_SET } from '../TrieUtils'; import { emptyMap } from '../Map'; +import { emptyList } from '../List'; import { get } from './get'; import { remove } from './remove'; import { set } from './set'; @@ -66,10 +67,18 @@ function updateInDeeply( return nextUpdated === nextExisting ? existing : nextUpdated === NOT_SET - ? remove(existing, key) - : set( - wasNotSet ? (inImmutable ? emptyMap() : {}) : existing, - key, - nextUpdated - ); + ? remove(existing, key) + : set( + wasNotSet + ? inImmutable + ? typeof key === 'number' + ? emptyList() + : emptyMap() + : typeof key === 'number' + ? [] + : {} + : existing, + key, + nextUpdated + ); }