Merge pull request #23517 from thorn0/angular-foreach-array-likes

AngularJS: improve types for `foreach` (support for array-likes)
This commit is contained in:
Daniel Rosenwasser
2018-02-15 12:50:30 -08:00
committed by GitHub
2 changed files with 23 additions and 1 deletions
+18
View File
@@ -710,6 +710,24 @@ function test_angular_forEach() {
this.push(`${key}: ${value}`);
}, log);
// expect(log).toEqual(['name: misko', 'gender: male']);
// $ExpectType NodeListOf<HTMLImageElement>
angular.forEach(document.getElementsByTagName('img'), (value, key, obj) => {
value; // $ExpectType HTMLImageElement
key; // $ExpectType number
obj; // $ExpectType NodeListOf<HTMLImageElement>
});
// $ExpectType number[]
angular.forEach([1, 2, 3], (value, key, obj) => {
value; // $ExpectType number
key; // $ExpectType number
obj; // $ExpectType number[]
});
// $ExpectType string
angular.forEach('123', (value, key, obj) => {
value; // $ExpectType string
key; // $ExpectType number
obj; // $ExpectType string
});
}
// angular.element() tests
+5 -1
View File
@@ -100,7 +100,11 @@ declare namespace angular {
* @param iterator Iterator function.
* @param context Object to become context (this) for the iterator function.
*/
forEach<T>(obj: T[], iterator: (value: T, key: number, obj: T[]) => void, context?: any): T[];
forEach<T, U extends ArrayLike<T> = T[]>(
obj: U,
iterator: (value: U[number], key: number, obj: U) => void,
context?: any
): U;
/**
* Invokes the iterator function once for each item in obj collection, which can be either an object or an array. The iterator function is invoked with iterator(value, key), where value is the value of an object property or an array element and key is the object property key or array element index. Specifying a context for the function is optional.
*