Skip to content

Commit dfef227

Browse files
Merge pull request #26473 from Microsoft/doGlobalImplicitThisRight
Fix bad message for captured global 'this'.
2 parents b983da5 + aba8290 commit dfef227

7 files changed

+63
-9
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15497,9 +15497,9 @@ namespace ts {
1549715497
// With noImplicitThis, functions may not reference 'this' if it has type 'any'
1549815498
error(
1549915499
node,
15500-
capturedByArrowFunction ?
15501-
Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation :
15502-
Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this_which_implicitly_has_type_any);
15500+
capturedByArrowFunction && container.kind === SyntaxKind.SourceFile ?
15501+
Diagnostics.The_containing_arrow_function_captures_the_global_value_of_this_which_implicitly_has_type_any :
15502+
Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation);
1550315503
}
1550415504
return type || anyType;
1550515505
}

tests/baselines/reference/noImplicitThisFunctions.errors.txt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
tests/cases/compiler/noImplicitThisFunctions.ts(13,12): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
2-
tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
1+
tests/cases/compiler/noImplicitThisFunctions.ts(13,12): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
2+
tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
3+
tests/cases/compiler/noImplicitThisFunctions.ts(18,22): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
4+
tests/cases/compiler/noImplicitThisFunctions.ts(20,36): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
5+
tests/cases/compiler/noImplicitThisFunctions.ts(21,50): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
36

47

5-
==== tests/cases/compiler/noImplicitThisFunctions.ts (2 errors) ====
8+
==== tests/cases/compiler/noImplicitThisFunctions.ts (5 errors) ====
69
function f1(x) {
710
// implicit any is still allowed
811
return x + 1;
@@ -17,11 +20,21 @@ tests/cases/compiler/noImplicitThisFunctions.ts(17,38): error TS2683: 'this' imp
1720
// error: this is implicitly any
1821
return this.a + z;
1922
~~~~
20-
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
23+
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
2124
}
2225

2326
// error: `this` is `window`, but is still of type `any`
2427
let f4: (b: number) => number = b => this.c + b;
2528
~~~~
29+
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
30+
let f5 = () => () => this;
31+
~~~~
32+
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
33+
34+
let f6 = function() { return () => this; };
35+
~~~~
36+
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
37+
let f7 = function() { return function() { return this } };
38+
~~~~
2639
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
2740

tests/baselines/reference/noImplicitThisFunctions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function f3(z: number): number {
1616

1717
// error: `this` is `window`, but is still of type `any`
1818
let f4: (b: number) => number = b => this.c + b;
19+
let f5 = () => () => this;
20+
21+
let f6 = function() { return () => this; };
22+
let f7 = function() { return function() { return this } };
1923

2024

2125
//// [noImplicitThisFunctions.js]
@@ -34,3 +38,9 @@ function f3(z) {
3438
}
3539
// error: `this` is `window`, but is still of type `any`
3640
var f4 = function (b) { return _this.c + b; };
41+
var f5 = function () { return function () { return _this; }; };
42+
var f6 = function () {
43+
var _this = this;
44+
return function () { return _this; };
45+
};
46+
var f7 = function () { return function () { return this; }; };

tests/baselines/reference/noImplicitThisFunctions.symbols

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ let f4: (b: number) => number = b => this.c + b;
3333
>b : Symbol(b, Decl(noImplicitThisFunctions.ts, 16, 31))
3434
>b : Symbol(b, Decl(noImplicitThisFunctions.ts, 16, 31))
3535

36+
let f5 = () => () => this;
37+
>f5 : Symbol(f5, Decl(noImplicitThisFunctions.ts, 17, 3))
38+
39+
let f6 = function() { return () => this; };
40+
>f6 : Symbol(f6, Decl(noImplicitThisFunctions.ts, 19, 3))
41+
42+
let f7 = function() { return function() { return this } };
43+
>f7 : Symbol(f7, Decl(noImplicitThisFunctions.ts, 20, 3))
44+

tests/baselines/reference/noImplicitThisFunctions.types

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ let f4: (b: number) => number = b => this.c + b;
4646
>c : any
4747
>b : number
4848

49+
let f5 = () => () => this;
50+
>f5 : () => () => any
51+
>() => () => this : () => () => any
52+
>() => this : () => any
53+
>this : any
54+
55+
let f6 = function() { return () => this; };
56+
>f6 : () => () => any
57+
>function() { return () => this; } : () => () => any
58+
>() => this : () => any
59+
>this : any
60+
61+
let f7 = function() { return function() { return this } };
62+
>f7 : () => () => any
63+
>function() { return function() { return this } } : () => () => any
64+
>function() { return this } : () => any
65+
>this : any
66+

tests/baselines/reference/thisBinding2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/thisBinding2.ts(10,11): error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
1+
tests/cases/compiler/thisBinding2.ts(10,11): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
22

33

44
==== tests/cases/compiler/thisBinding2.ts (1 errors) ====
@@ -13,7 +13,7 @@ tests/cases/compiler/thisBinding2.ts(10,11): error TS7041: The containing arrow
1313
var x = 1;
1414
return this.x;
1515
~~~~
16-
!!! error TS7041: The containing arrow function captures the global value of 'this' which implicitly has type 'any'.
16+
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
1717
}();
1818
}
1919
}

tests/cases/compiler/noImplicitThisFunctions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ function f3(z: number): number {
1717

1818
// error: `this` is `window`, but is still of type `any`
1919
let f4: (b: number) => number = b => this.c + b;
20+
let f5 = () => () => this;
21+
22+
let f6 = function() { return () => this; };
23+
let f7 = function() { return function() { return this } };

0 commit comments

Comments
 (0)