Skip to content

Commit bee188b

Browse files
committed
Strengthen type of methods accepting thisArg.
Strengthen forEach, filter, map, from, find, findIndex to be more stricter, so that the TypeScript compiler can understand the type of this when thisArg is used but the predicate does not explicitly mention the type of this and only be implicit. Although methods like some, every also have the thisArg as its parameter but is not included in this change, because in some places those are called like e.g. array.some(isNaN) and in these cases, the TSC does not understand the type of the isNaN is actually a subtype. Also, this excludes to improve the methods cases where the interface has overloads of the same method, due to the bug microsoft#44373, as the current TSC cannot resolve multiple declarations properly for cases which these functions are called on a union of types which all are arrays. e.g. (string[]|number[]).forEach(predicate, ...);
1 parent 4635a5c commit bee188b

5 files changed

+89
-89
lines changed

lib/lib.es2015.collection.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface Map<K, V> {
2828
/**
2929
* Executes a provided function once per each key/value pair in the Map, in insertion order.
3030
*/
31-
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void;
31+
forEach<T>(callbackfn: (this: T, value: V, key: K, map: Map<K, V>) => void, thisArg?: T): void;
3232
/**
3333
* Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
3434
* @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
@@ -56,7 +56,7 @@ interface MapConstructor {
5656
declare var Map: MapConstructor;
5757

5858
interface ReadonlyMap<K, V> {
59-
forEach(callbackfn: (value: V, key: K, map: ReadonlyMap<K, V>) => void, thisArg?: any): void;
59+
forEach<T>(callbackfn: (this: T, value: V, key: K, map: ReadonlyMap<K, V>) => void, thisArg?: T): void;
6060
get(key: K): V | undefined;
6161
has(key: K): boolean;
6262
readonly size: number;
@@ -104,7 +104,7 @@ interface Set<T> {
104104
/**
105105
* Executes a provided function once per each value in the Set object, in insertion order.
106106
*/
107-
forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: any): void;
107+
forEach<K>(callbackfn: (this: K, value: T, value2: T, set: Set<T>) => void, thisArg?: K): void;
108108
/**
109109
* @returns a boolean indicating whether an element with the specified value exists in the Set or not.
110110
*/
@@ -122,7 +122,7 @@ interface SetConstructor {
122122
declare var Set: SetConstructor;
123123

124124
interface ReadonlySet<T> {
125-
forEach(callbackfn: (value: T, value2: T, set: ReadonlySet<T>) => void, thisArg?: any): void;
125+
forEach<K>(callbackfn: (this: K, value: T, value2: T, set: ReadonlySet<T>) => void, thisArg?: K): void;
126126
has(value: T): boolean;
127127
readonly size: number;
128128
}

lib/lib.es2015.iterable.d.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ interface ArrayConstructor {
8888
* @param mapfn A mapping function to call on every element of the array.
8989
* @param thisArg Value of 'this' used to invoke the mapfn.
9090
*/
91-
from<T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[];
91+
from<T, U, K = undefined>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (this: K, v: T, k: number) => U, thisArg?: K): U[];
9292
}
9393

9494
interface ReadonlyArray<T> {
@@ -265,7 +265,7 @@ interface Int8ArrayConstructor {
265265
* @param mapfn A mapping function to call on every element of the array.
266266
* @param thisArg Value of 'this' used to invoke the mapfn.
267267
*/
268-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int8Array;
268+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Int8Array;
269269
}
270270

271271
interface Uint8Array {
@@ -293,7 +293,7 @@ interface Uint8ArrayConstructor {
293293
* @param mapfn A mapping function to call on every element of the array.
294294
* @param thisArg Value of 'this' used to invoke the mapfn.
295295
*/
296-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8Array;
296+
from<T>(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: T): Uint8Array;
297297
}
298298

299299
interface Uint8ClampedArray {
@@ -324,7 +324,7 @@ interface Uint8ClampedArrayConstructor {
324324
* @param mapfn A mapping function to call on every element of the array.
325325
* @param thisArg Value of 'this' used to invoke the mapfn.
326326
*/
327-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8ClampedArray;
327+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Uint8ClampedArray;
328328
}
329329

330330
interface Int16Array {
@@ -354,7 +354,7 @@ interface Int16ArrayConstructor {
354354
* @param mapfn A mapping function to call on every element of the array.
355355
* @param thisArg Value of 'this' used to invoke the mapfn.
356356
*/
357-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int16Array;
357+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Int16Array;
358358
}
359359

360360
interface Uint16Array {
@@ -382,7 +382,7 @@ interface Uint16ArrayConstructor {
382382
* @param mapfn A mapping function to call on every element of the array.
383383
* @param thisArg Value of 'this' used to invoke the mapfn.
384384
*/
385-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint16Array;
385+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Uint16Array;
386386
}
387387

388388
interface Int32Array {
@@ -410,7 +410,7 @@ interface Int32ArrayConstructor {
410410
* @param mapfn A mapping function to call on every element of the array.
411411
* @param thisArg Value of 'this' used to invoke the mapfn.
412412
*/
413-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int32Array;
413+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Int32Array;
414414
}
415415

416416
interface Uint32Array {
@@ -438,7 +438,7 @@ interface Uint32ArrayConstructor {
438438
* @param mapfn A mapping function to call on every element of the array.
439439
* @param thisArg Value of 'this' used to invoke the mapfn.
440440
*/
441-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint32Array;
441+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Uint32Array;
442442
}
443443

444444
interface Float32Array {
@@ -466,7 +466,7 @@ interface Float32ArrayConstructor {
466466
* @param mapfn A mapping function to call on every element of the array.
467467
* @param thisArg Value of 'this' used to invoke the mapfn.
468468
*/
469-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float32Array;
469+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Float32Array;
470470
}
471471

472472
interface Float64Array {
@@ -494,5 +494,5 @@ interface Float64ArrayConstructor {
494494
* @param mapfn A mapping function to call on every element of the array.
495495
* @param thisArg Value of 'this' used to invoke the mapfn.
496496
*/
497-
from(arrayLike: Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float64Array;
497+
from<T>(arrayLike: Iterable<number>, mapfn?: (this: T, v: number, k: number) => number, thisArg?: T): Float64Array;
498498
}

lib/lib.es2020.bigint.d.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ interface BigInt64Array {
202202
* @param thisArg An object to which the this keyword can refer in the predicate function.
203203
* If thisArg is omitted, undefined is used as the this value.
204204
*/
205-
filter(predicate: (value: bigint, index: number, array: BigInt64Array) => any, thisArg?: any): BigInt64Array;
205+
filter<T>(predicate: (this: T, value: bigint, index: number, array: BigInt64Array) => any, thisArg?: T): BigInt64Array;
206206

207207
/**
208208
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -213,7 +213,7 @@ interface BigInt64Array {
213213
* @param thisArg If provided, it will be used as the this value for each invocation of
214214
* predicate. If it is not provided, undefined is used instead.
215215
*/
216-
find(predicate: (value: bigint, index: number, array: BigInt64Array) => boolean, thisArg?: any): bigint | undefined;
216+
find<T>(predicate: (this: T, value: bigint, index: number, array: BigInt64Array) => boolean, thisArg?: T): bigint | undefined;
217217

218218
/**
219219
* Returns the index of the first element in the array where predicate is true, and -1
@@ -224,7 +224,7 @@ interface BigInt64Array {
224224
* @param thisArg If provided, it will be used as the this value for each invocation of
225225
* predicate. If it is not provided, undefined is used instead.
226226
*/
227-
findIndex(predicate: (value: bigint, index: number, array: BigInt64Array) => boolean, thisArg?: any): number;
227+
findIndex<T>(predicate: (this: T, value: bigint, index: number, array: BigInt64Array) => boolean, thisArg?: T): number;
228228

229229
/**
230230
* Performs the specified action for each element in an array.
@@ -233,7 +233,7 @@ interface BigInt64Array {
233233
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
234234
* If thisArg is omitted, undefined is used as the this value.
235235
*/
236-
forEach(callbackfn: (value: bigint, index: number, array: BigInt64Array) => void, thisArg?: any): void;
236+
forEach<T>(callbackfn: (this: T, value: bigint, index: number, array: BigInt64Array) => void, thisArg?: T): void;
237237

238238
/**
239239
* Determines whether an array includes a certain element, returning true or false as appropriate.
@@ -279,7 +279,7 @@ interface BigInt64Array {
279279
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
280280
* If thisArg is omitted, undefined is used as the this value.
281281
*/
282-
map(callbackfn: (value: bigint, index: number, array: BigInt64Array) => bigint, thisArg?: any): BigInt64Array;
282+
map<T>(callbackfn: (this: T, value: bigint, index: number, array: BigInt64Array) => bigint, thisArg?: T): BigInt64Array;
283283

284284
/**
285285
* Calls the specified callback function for all the elements in an array. The return value of
@@ -411,7 +411,7 @@ interface BigInt64ArrayConstructor {
411411
* @param thisArg Value of 'this' used to invoke the mapfn.
412412
*/
413413
from(arrayLike: ArrayLike<bigint>): BigInt64Array;
414-
from<U>(arrayLike: ArrayLike<U>, mapfn: (v: U, k: number) => bigint, thisArg?: any): BigInt64Array;
414+
from<U, T = undefined>(arrayLike: ArrayLike<U>, mapfn: (this: T, v: U, k: number) => bigint, thisArg?: T): BigInt64Array;
415415
}
416416

417417
declare var BigInt64Array: BigInt64ArrayConstructor;
@@ -474,7 +474,7 @@ interface BigUint64Array {
474474
* @param thisArg An object to which the this keyword can refer in the predicate function.
475475
* If thisArg is omitted, undefined is used as the this value.
476476
*/
477-
filter(predicate: (value: bigint, index: number, array: BigUint64Array) => any, thisArg?: any): BigUint64Array;
477+
filter<T>(predicate: (this: T, value: bigint, index: number, array: BigUint64Array) => any, thisArg?: T): BigUint64Array;
478478

479479
/**
480480
* Returns the value of the first element in the array where predicate is true, and undefined
@@ -485,7 +485,7 @@ interface BigUint64Array {
485485
* @param thisArg If provided, it will be used as the this value for each invocation of
486486
* predicate. If it is not provided, undefined is used instead.
487487
*/
488-
find(predicate: (value: bigint, index: number, array: BigUint64Array) => boolean, thisArg?: any): bigint | undefined;
488+
find<T>(predicate: (this: T, value: bigint, index: number, array: BigUint64Array) => boolean, thisArg?: T): bigint | undefined;
489489

490490
/**
491491
* Returns the index of the first element in the array where predicate is true, and -1
@@ -496,7 +496,7 @@ interface BigUint64Array {
496496
* @param thisArg If provided, it will be used as the this value for each invocation of
497497
* predicate. If it is not provided, undefined is used instead.
498498
*/
499-
findIndex(predicate: (value: bigint, index: number, array: BigUint64Array) => boolean, thisArg?: any): number;
499+
findIndex<T>(predicate: (this: T, value: bigint, index: number, array: BigUint64Array) => boolean, thisArg?: T): number;
500500

501501
/**
502502
* Performs the specified action for each element in an array.
@@ -505,7 +505,7 @@ interface BigUint64Array {
505505
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
506506
* If thisArg is omitted, undefined is used as the this value.
507507
*/
508-
forEach(callbackfn: (value: bigint, index: number, array: BigUint64Array) => void, thisArg?: any): void;
508+
forEach<T>(callbackfn: (this: T, value: bigint, index: number, array: BigUint64Array) => void, thisArg?: T): void;
509509

510510
/**
511511
* Determines whether an array includes a certain element, returning true or false as appropriate.
@@ -551,7 +551,7 @@ interface BigUint64Array {
551551
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
552552
* If thisArg is omitted, undefined is used as the this value.
553553
*/
554-
map(callbackfn: (value: bigint, index: number, array: BigUint64Array) => bigint, thisArg?: any): BigUint64Array;
554+
map<T>(callbackfn: (this: T, value: bigint, index: number, array: BigUint64Array) => bigint, thisArg?: T): BigUint64Array;
555555

556556
/**
557557
* Calls the specified callback function for all the elements in an array. The return value of
@@ -683,7 +683,7 @@ interface BigUint64ArrayConstructor {
683683
* @param thisArg Value of 'this' used to invoke the mapfn.
684684
*/
685685
from(arrayLike: ArrayLike<bigint>): BigUint64Array;
686-
from<U>(arrayLike: ArrayLike<U>, mapfn: (v: U, k: number) => bigint, thisArg?: any): BigUint64Array;
686+
from<U, T = undefined>(arrayLike: ArrayLike<U>, mapfn: (this: T, v: U, k: number) => bigint, thisArg?: T): BigUint64Array;
687687
}
688688

689689
declare var BigUint64Array: BigUint64ArrayConstructor;

0 commit comments

Comments
 (0)