Skip to content

Commit 0113ad5

Browse files
committed
Error on all uses of this that are implicitly any
Previously it was only an error inside an function.
1 parent f64110a commit 0113ad5

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8110,7 +8110,7 @@ namespace ts {
81108110
}
81118111
}
81128112

8113-
if (compilerOptions.noImplicitThis && isFunctionLike(container)) {
8113+
if (compilerOptions.noImplicitThis) {
81148114
// With noImplicitThis, functions may not reference 'this' if it has type 'any'
81158115
error(node, Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
81168116
}

tests/baselines/reference/noImplicitThisFunctions.errors.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
tests/cases/compiler/noImplicitThisFunctions.ts(14,12): error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
2+
tests/cases/compiler/noImplicitThisFunctions.ts(18,38): error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
23

34

4-
==== tests/cases/compiler/noImplicitThisFunctions.ts (1 errors) ====
5+
==== tests/cases/compiler/noImplicitThisFunctions.ts (2 errors) ====
56

67
function f1(x) {
78
// implicit any is still allowed
@@ -20,6 +21,8 @@ tests/cases/compiler/noImplicitThisFunctions.ts(14,12): error TS2681: 'this' imp
2021
!!! error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
2122
}
2223

23-
// ok, arrow functions don't even bind `this`, so `this` is just `window`
24+
// error: `this` is `window`, but is still of type `any`
2425
let f4: (b: number) => number = b => this.c + b;
26+
~~~~
27+
!!! error TS2681: 'this' implicitly has type 'any' because it does not have a type annotation.
2528

tests/baselines/reference/noImplicitThisFunctions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function f3(z: number): number {
1515
return this.a + z;
1616
}
1717

18-
// ok, arrow functions don't even bind `this`, so `this` is just `window`
18+
// error: `this` is `window`, but is still of type `any`
1919
let f4: (b: number) => number = b => this.c + b;
2020

2121

@@ -33,5 +33,5 @@ function f3(z) {
3333
// error: this is implicitly any
3434
return this.a + z;
3535
}
36-
// ok, arrow functions don't even bind `this`, so `this` is just `window`
36+
// error: `this` is `window`, but is still of type `any`
3737
var f4 = function (b) { return _this.c + b; };

tests/cases/compiler/noImplicitThisFunctions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ function f3(z: number): number {
1515
return this.a + z;
1616
}
1717

18-
// ok, arrow functions don't even bind `this`, so `this` is just `window`
18+
// error: `this` is `window`, but is still of type `any`
1919
let f4: (b: number) => number = b => this.c + b;

0 commit comments

Comments
 (0)