@@ -72,6 +72,54 @@ describe('updateIn', () => {
72
72
) ;
73
73
} ) ;
74
74
75
+ it ( 'handle ArrayLike objects that are nor Array not immutable Collection' , ( ) => {
76
+ class CustomArrayLike < T > implements ArrayLike < T > {
77
+ readonly length : number ;
78
+ [ n : number ] : T ;
79
+
80
+ constructor ( ...values : Array < T > ) {
81
+ this . length = values . length ;
82
+
83
+ for ( let i = 0 ; i < values . length ; i ++ ) {
84
+ this [ i ] = values [ i ] ;
85
+ }
86
+ }
87
+
88
+ // Define other methods if needed, but do not include the `slice` method
89
+ // For example, you can define a method to set values
90
+ set ( index : number , value : T ) : void {
91
+ if ( index < 0 || index >= this . length ) {
92
+ throw new RangeError ( 'Index out of bounds' ) ;
93
+ }
94
+
95
+ this [ index ] = value ;
96
+ }
97
+
98
+ // Define a method to get values
99
+ get ( index : number ) : T {
100
+ if ( index < 0 || index >= this . length ) {
101
+ throw new RangeError ( 'Index out of bounds' ) ;
102
+ }
103
+
104
+ return this [ index ] ;
105
+ }
106
+ }
107
+
108
+ // create an ArrayLike
109
+ const customArray = new CustomArrayLike < number > ( 10 , 20 ) ;
110
+
111
+ // code that works perfectly
112
+ expect (
113
+ // @ts -expect-error -- `updateIn` keypath type should be `OrderedCollection<K> | ArrayLike<K>;
114
+ updateIn ( { 10 : { 20 : 'a' } } , customArray , ( v ) => `${ v . toUpperCase ( ) } ` )
115
+ ) . toEqual ( { 10 : { 20 : 'A' } } ) ;
116
+
117
+ expect ( ( ) =>
118
+ // @ts -expect-error -- `updateIn` keypath type should be `OrderedCollection<K> | ArrayLike<K>;
119
+ updateIn ( { 10 : 'a' } , customArray , ( v ) => `${ v . toUpperCase ( ) } ` )
120
+ ) . toThrow ( 'Cannot update within non-data-structure value in path [10]: a' ) ;
121
+ } ) ;
122
+
75
123
it ( 'identity with notSetValue is still identity' , ( ) => {
76
124
const m = Map ( { a : { b : { c : 10 } } } ) ;
77
125
expect ( m . updateIn ( [ 'x' ] , 100 , ( id ) => id ) ) . toEqual ( m ) ;
@@ -159,7 +207,9 @@ describe('updateIn', () => {
159
207
const m = fromJS ( { a : { b : { c : 10 } } } ) ;
160
208
expect ( ( ) => {
161
209
m . updateIn ( [ 'a' , 'b' , 'c' , 'd' ] , ( ) => 20 ) . toJS ( ) ;
162
- } ) . toThrow ( ) ;
210
+ } ) . toThrow (
211
+ 'Cannot update within non-data-structure value in path ["a","b","c"]: 10'
212
+ ) ;
163
213
} ) ;
164
214
165
215
it ( 'update with notSetValue when non-existing key' , ( ) => {
0 commit comments