From 498fee439f3bf34098261ff03aed2351210ea5b4 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 19 Dec 2016 13:39:30 -0800 Subject: [PATCH] array-foreach: Add `this` type --- array-foreach/array-foreach-tests.ts | 6 +++--- array-foreach/index.d.ts | 8 +++++--- array-foreach/tsconfig.json | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/array-foreach/array-foreach-tests.ts b/array-foreach/array-foreach-tests.ts index cc75ff25ae..02c1d2dadd 100644 --- a/array-foreach/array-foreach-tests.ts +++ b/array-foreach/array-foreach-tests.ts @@ -17,21 +17,21 @@ arrayForEach(array, (i: number, index: number, array: Array) => { const resultThis: Array<{i: number, that: string}> = []; -arrayForEach(array, (i: number) => { +arrayForEach(array, function(i: number) { resultThis.push({ i: i, that: this.that }); }, { that: 'jeff' }); -arrayForEach(array, (i: number, index: number) => { +arrayForEach(array, function(i: number, index: number) { resultThis.push({ i: i + index, that: this.that }); }, { that: 'jeff' }); -arrayForEach(array, (i: number, index: number, array: Array) => { +arrayForEach(array, function(i: number, index: number, array: Array) { resultThis.push({ i: array[i], that: this.that diff --git a/array-foreach/index.d.ts b/array-foreach/index.d.ts index 6493ebd3ec..023b8168b7 100644 --- a/array-foreach/index.d.ts +++ b/array-foreach/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for array-foreach +// Type definitions for array-foreach 1.0 // Project: https://www.npmjs.com/package/array-foreach // Definitions by: Steve Jenkins // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -9,7 +9,8 @@ * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ -declare function forEach(arr: Array, callbackfn: (value: T, index: number, array: ReadonlyArray) => void, thisArg?: any): void; +declare function forEach(arr: T[], callbackfn: (value: T, index: number, array: ReadonlyArray) => void): void; +declare function forEach(arr: T[], callbackfn: (this: U, value: T, index: number, array: ReadonlyArray) => void, thisArg: U): void; /** * Performs the specified action for each element in an array. @@ -17,5 +18,6 @@ declare function forEach(arr: Array, callbackfn: (value: T, index: number, * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ -declare function forEach(arr: NodeListOf, callbackfn: (value: T, index: number, array: ReadonlyArray) => void, thisArg?: any): void; +declare function forEach(arr: NodeListOf, callbackfn: (value: T, index: number, array: ReadonlyArray) => void): void; +declare function forEach(arr: NodeListOf, callbackfn: (this: U, value: T, index: number, array: ReadonlyArray) => void, thisArg: U): void; export = forEach; diff --git a/array-foreach/tsconfig.json b/array-foreach/tsconfig.json index 5fe9f2ab1a..6b42555b56 100644 --- a/array-foreach/tsconfig.json +++ b/array-foreach/tsconfig.json @@ -3,7 +3,7 @@ "module": "commonjs", "target": "es6", "noImplicitAny": true, - "noImplicitThis": false, + "noImplicitThis": true, "strictNullChecks": true, "baseUrl": "../", "typeRoots": [