Skip to content

Commit d1b60f8

Browse files
committed
Merge pull request #8389 from Microsoft/make-object-literal-this-any-typed
Make `this` in object literal always of type `any`
2 parents f0e2d81 + 8d45a73 commit d1b60f8

33 files changed

+156
-206
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8228,15 +8228,6 @@ namespace ts {
82288228
if (signature.thisType) {
82298229
return signature.thisType;
82308230
}
8231-
const parentObject = container.parent && container.parent.kind === SyntaxKind.PropertyAssignment ? container.parent.parent : container.parent;
8232-
if (parentObject && parentObject.kind === SyntaxKind.ObjectLiteralExpression) {
8233-
// Note: this works because object literal methods are deferred,
8234-
// which means that the type of the containing object literal is already known.
8235-
const type = checkExpressionCached(<ObjectLiteralExpression>parentObject);
8236-
if (type) {
8237-
return type;
8238-
}
8239-
}
82408231
}
82418232
if (isClassLike(container.parent)) {
82428233
const symbol = getSymbolOfNode(container.parent);
@@ -8476,10 +8467,7 @@ namespace ts {
84768467
if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== SyntaxKind.ArrowFunction) {
84778468
const contextualSignature = getContextualSignature(func);
84788469
if (contextualSignature) {
8479-
return contextualSignature.thisType || anyType;
8480-
}
8481-
else if (getContextualTypeForFunctionLikeDeclaration(func) === anyType) {
8482-
return anyType;
8470+
return contextualSignature.thisType;
84838471
}
84848472
}
84858473

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
tests/cases/compiler/commentsOnObjectLiteral2.ts(1,14): error TS2304: Cannot find name 'makeClass'.
2-
tests/cases/compiler/commentsOnObjectLiteral2.ts(9,17): error TS2339: Property 'name' does not exist on type '{ initialize: (name: any) => void; }'.
32

43

5-
==== tests/cases/compiler/commentsOnObjectLiteral2.ts (2 errors) ====
4+
==== tests/cases/compiler/commentsOnObjectLiteral2.ts (1 errors) ====
65
var Person = makeClass(
76
~~~~~~~~~
87
!!! error TS2304: Cannot find name 'makeClass'.
@@ -14,8 +13,6 @@ tests/cases/compiler/commentsOnObjectLiteral2.ts(9,17): error TS2339: Property '
1413
*/
1514
initialize: function(name) {
1615
this.name = name;
17-
~~~~
18-
!!! error TS2339: Property 'name' does not exist on type '{ initialize: (name: any) => void; }'.
1916
} /* trailing comment 1*/,
2017
}
2118
);

tests/baselines/reference/commentsOnObjectLiteral3.symbols

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,13 @@ var v = {
2121
>a : Symbol(a, Decl(commentsOnObjectLiteral3.ts, 8, 13), Decl(commentsOnObjectLiteral3.ts, 12, 18))
2222

2323
return this.prop;
24-
>this.prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))
25-
>this : Symbol(, Decl(commentsOnObjectLiteral3.ts, 1, 7))
26-
>prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))
27-
2824
} /*trailing 1*/,
2925
//setter
3026
set a(value) {
3127
>a : Symbol(a, Decl(commentsOnObjectLiteral3.ts, 8, 13), Decl(commentsOnObjectLiteral3.ts, 12, 18))
3228
>value : Symbol(value, Decl(commentsOnObjectLiteral3.ts, 14, 7))
3329

3430
this.prop = value;
35-
>this.prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))
36-
>this : Symbol(, Decl(commentsOnObjectLiteral3.ts, 1, 7))
37-
>prop : Symbol(prop, Decl(commentsOnObjectLiteral3.ts, 1, 9))
3831
>value : Symbol(value, Decl(commentsOnObjectLiteral3.ts, 14, 7))
3932

4033
} // trailing 2

tests/baselines/reference/commentsOnObjectLiteral3.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ var v = {
2424
>a : any
2525

2626
return this.prop;
27-
>this.prop : number
28-
>this : { prop: number; func: () => void; func1(): void; a: any; }
29-
>prop : number
27+
>this.prop : any
28+
>this : any
29+
>prop : any
3030

3131
} /*trailing 1*/,
3232
//setter
@@ -36,9 +36,9 @@ var v = {
3636

3737
this.prop = value;
3838
>this.prop = value : any
39-
>this.prop : number
40-
>this : { prop: number; func: () => void; func1(): void; a: any; }
41-
>prop : number
39+
>this.prop : any
40+
>this : any
41+
>prop : any
4242
>value : any
4343

4444
} // trailing 2

tests/baselines/reference/declFileObjectLiteralWithAccessors.symbols

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ function /*1*/makePoint(x: number) {
1515
set x(a: number) { this.b = a; }
1616
>x : Symbol(x, Decl(declFileObjectLiteralWithAccessors.ts, 3, 14), Decl(declFileObjectLiteralWithAccessors.ts, 4, 30))
1717
>a : Symbol(a, Decl(declFileObjectLiteralWithAccessors.ts, 5, 14))
18-
>this.b : Symbol(b, Decl(declFileObjectLiteralWithAccessors.ts, 2, 12))
19-
>this : Symbol(, Decl(declFileObjectLiteralWithAccessors.ts, 2, 10))
20-
>b : Symbol(b, Decl(declFileObjectLiteralWithAccessors.ts, 2, 12))
2118
>a : Symbol(a, Decl(declFileObjectLiteralWithAccessors.ts, 5, 14))
2219

2320
};

tests/baselines/reference/declFileObjectLiteralWithAccessors.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function /*1*/makePoint(x: number) {
1919
>x : number
2020
>a : number
2121
>this.b = a : number
22-
>this.b : number
23-
>this : { b: number; x: number; }
24-
>b : number
22+
>this.b : any
23+
>this : any
24+
>b : any
2525
>a : number
2626

2727
};

tests/baselines/reference/declFileObjectLiteralWithOnlySetter.symbols

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ function /*1*/makePoint(x: number) {
1111
set x(a: number) { this.b = a; }
1212
>x : Symbol(x, Decl(declFileObjectLiteralWithOnlySetter.ts, 3, 14))
1313
>a : Symbol(a, Decl(declFileObjectLiteralWithOnlySetter.ts, 4, 14))
14-
>this.b : Symbol(b, Decl(declFileObjectLiteralWithOnlySetter.ts, 2, 12))
15-
>this : Symbol(, Decl(declFileObjectLiteralWithOnlySetter.ts, 2, 10))
16-
>b : Symbol(b, Decl(declFileObjectLiteralWithOnlySetter.ts, 2, 12))
1714
>a : Symbol(a, Decl(declFileObjectLiteralWithOnlySetter.ts, 4, 14))
1815

1916
};

tests/baselines/reference/declFileObjectLiteralWithOnlySetter.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function /*1*/makePoint(x: number) {
1515
>x : number
1616
>a : number
1717
>this.b = a : number
18-
>this.b : number
19-
>this : { b: number; x: number; }
20-
>b : number
18+
>this.b : any
19+
>this : any
20+
>b : any
2121
>a : number
2222

2323
};
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts(9,10): error TS2526: A 'this' type is available only in a non-static member of a class or interface.
2-
tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts(10,19): error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
3-
Property 'a' is missing in type '{ m(): this is Foo; }'.
42

53

6-
==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts (2 errors) ====
4+
==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicates02.ts (1 errors) ====
75

86
export interface Foo {
97
a: string;
@@ -16,9 +14,6 @@ tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredic
1614
~~~~
1715
!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface.
1816
let dis = this as Foo;
19-
~~~~~~~~~~~
20-
!!! error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
21-
!!! error TS2352: Property 'a' is missing in type '{ m(): this is Foo; }'.
2217
return dis.a != null && dis.b != null && dis.c != null;
2318
}
2419
}

tests/baselines/reference/declarationEmitThisPredicatesWithPrivateName02.errors.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts(8,14): error TS4025: Exported variable 'obj' has or is using private name 'Foo'.
22
tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts(9,10): error TS2526: A 'this' type is available only in a non-static member of a class or interface.
3-
tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts(10,19): error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
4-
Property 'a' is missing in type '{ m(): this is Foo; }'.
53

64

7-
==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts (3 errors) ====
5+
==== tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredicatesWithPrivateName02.ts (2 errors) ====
86

97
interface Foo {
108
a: string;
@@ -19,9 +17,6 @@ tests/cases/conformance/declarationEmit/typePredicates/declarationEmitThisPredic
1917
~~~~
2018
!!! error TS2526: A 'this' type is available only in a non-static member of a class or interface.
2119
let dis = this as Foo;
22-
~~~~~~~~~~~
23-
!!! error TS2352: Type '{ m(): this is Foo; }' cannot be converted to type 'Foo'.
24-
!!! error TS2352: Property 'a' is missing in type '{ m(): this is Foo; }'.
2520
return dis.a != null && dis.b != null && dis.c != null;
2621
}
2722
}

tests/baselines/reference/emitCompoundExponentiationAssignmentWithIndexingOnLHS3.symbols

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,11 @@ var object = {
88

99
get 0() {
1010
return this._0;
11-
>this._0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))
12-
>this : Symbol(, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 12))
13-
>_0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))
14-
1511
},
1612
set 0(x: number) {
1713
>x : Symbol(x, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 6, 10))
1814

1915
this._0 = x;
20-
>this._0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))
21-
>this : Symbol(, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 12))
22-
>_0 : Symbol(_0, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 1, 14))
2316
>x : Symbol(x, Decl(emitCompoundExponentiationAssignmentWithIndexingOnLHS3.ts, 6, 10))
2417

2518
},

tests/baselines/reference/emitCompoundExponentiationAssignmentWithIndexingOnLHS3.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ var object = {
1010

1111
get 0() {
1212
return this._0;
13-
>this._0 : number
14-
>this : { 0: number; _0: number; }
15-
>_0 : number
13+
>this._0 : any
14+
>this : any
15+
>_0 : any
1616

1717
},
1818
set 0(x: number) {
1919
>x : number
2020

2121
this._0 = x;
2222
>this._0 = x : number
23-
>this._0 : number
24-
>this : { 0: number; _0: number; }
25-
>_0 : number
23+
>this._0 : any
24+
>this : any
25+
>_0 : any
2626
>x : number
2727

2828
},

tests/baselines/reference/fatarrowfunctions.symbols

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,6 @@ var messenger = {
163163

164164
setTimeout(() => { this.message.toString(); }, 3000);
165165
>setTimeout : Symbol(setTimeout, Decl(fatarrowfunctions.ts, 34, 1))
166-
>this.message.toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
167-
>this.message : Symbol(message, Decl(fatarrowfunctions.ts, 38, 17))
168-
>this : Symbol(, Decl(fatarrowfunctions.ts, 38, 15))
169-
>message : Symbol(message, Decl(fatarrowfunctions.ts, 38, 17))
170-
>toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
171166
}
172167
};
173168

tests/baselines/reference/fatarrowfunctions.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ var messenger = {
234234
>setTimeout(() => { this.message.toString(); }, 3000) : number
235235
>setTimeout : (expression: any, msec?: number, language?: any) => number
236236
>() => { this.message.toString(); } : () => void
237-
>this.message.toString() : string
238-
>this.message.toString : () => string
239-
>this.message : string
240-
>this : { message: string; start: () => void; }
241-
>message : string
242-
>toString : () => string
237+
>this.message.toString() : any
238+
>this.message.toString : any
239+
>this.message : any
240+
>this : any
241+
>message : any
242+
>toString : any
243243
>3000 : number
244244
}
245245
};

tests/baselines/reference/fatarrowfunctionsInFunctions.symbols

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ var messenger = {
1616

1717
var _self = this;
1818
>_self : Symbol(_self, Decl(fatarrowfunctionsInFunctions.ts, 5, 11))
19-
>this : Symbol(, Decl(fatarrowfunctionsInFunctions.ts, 2, 15))
2019

2120
setTimeout(function() {
2221
>setTimeout : Symbol(setTimeout, Decl(fatarrowfunctionsInFunctions.ts, 0, 0))
2322

2423
_self.message.toString();
25-
>_self.message.toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
26-
>_self.message : Symbol(message, Decl(fatarrowfunctionsInFunctions.ts, 2, 17))
2724
>_self : Symbol(_self, Decl(fatarrowfunctionsInFunctions.ts, 5, 11))
28-
>message : Symbol(message, Decl(fatarrowfunctionsInFunctions.ts, 2, 17))
29-
>toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
3025

3126
}, 3000);
3227
}

tests/baselines/reference/fatarrowfunctionsInFunctions.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ var messenger = {
1818
>function() { var _self = this; setTimeout(function() { _self.message.toString(); }, 3000); } : () => void
1919

2020
var _self = this;
21-
>_self : { message: string; start: () => void; }
22-
>this : { message: string; start: () => void; }
21+
>_self : any
22+
>this : any
2323

2424
setTimeout(function() {
2525
>setTimeout(function() { _self.message.toString(); }, 3000) : number
2626
>setTimeout : (expression: any, msec?: number, language?: any) => number
2727
>function() { _self.message.toString(); } : () => void
2828

2929
_self.message.toString();
30-
>_self.message.toString() : string
31-
>_self.message.toString : () => string
32-
>_self.message : string
33-
>_self : { message: string; start: () => void; }
34-
>message : string
35-
>toString : () => string
30+
>_self.message.toString() : any
31+
>_self.message.toString : any
32+
>_self.message : any
33+
>_self : any
34+
>message : any
35+
>toString : any
3636

3737
}, 3000);
3838
>3000 : number

tests/baselines/reference/looseThisTypeInFunctions.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(21,1): error TS2322: Type '(this: C, m: number) => number' is not assignable to type '(this: void, m: number) => number'.
22
The 'this' types of each signature are incompatible.
33
Type 'void' is not assignable to type 'C'.
4-
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(25,27): error TS2339: Property 'length' does not exist on type 'number'.
54
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(33,28): error TS2339: Property 'length' does not exist on type 'number'.
65
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(37,9): error TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'I'.
76
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(46,20): error TS2339: Property 'length' does not exist on type 'number'.
87

98

10-
==== tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts (5 errors) ====
9+
==== tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts (4 errors) ====
1110
interface I {
1211
n: number;
1312
explicitThis(this: this, m: number): number;
@@ -37,8 +36,6 @@ tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(46,20): error
3736
n: 101,
3837
explicitThis: function (m: number) {
3938
return m + this.n.length; // error, 'length' does not exist on 'number'
40-
~~~~~~
41-
!!! error TS2339: Property 'length' does not exist on type 'number'.
4239
},
4340
implicitThis(m: number): number { return m; }
4441
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/noImplicitThisObjectLiterals.ts(2,8): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
2+
tests/cases/compiler/noImplicitThisObjectLiterals.ts(4,16): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
3+
tests/cases/compiler/noImplicitThisObjectLiterals.ts(7,16): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
4+
5+
6+
==== tests/cases/compiler/noImplicitThisObjectLiterals.ts (3 errors) ====
7+
let o = {
8+
d: this, // error, this: any
9+
~~~~
10+
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
11+
m() {
12+
return this.d.length; // error, this: any
13+
~~~~
14+
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
15+
},
16+
f: function() {
17+
return this.d.length; // error, this: any
18+
~~~~
19+
!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
20+
}
21+
}
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [noImplicitThisObjectLiterals.ts]
2+
let o = {
3+
d: this, // error, this: any
4+
m() {
5+
return this.d.length; // error, this: any
6+
},
7+
f: function() {
8+
return this.d.length; // error, this: any
9+
}
10+
}
11+
12+
13+
//// [noImplicitThisObjectLiterals.js]
14+
var o = {
15+
d: this,
16+
m: function () {
17+
return this.d.length; // error, this: any
18+
},
19+
f: function () {
20+
return this.d.length; // error, this: any
21+
}
22+
};

tests/baselines/reference/selfInLambdas.symbols

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,8 @@ var o = {
3737
>onmousemove : Symbol(Window.onmousemove, Decl(selfInLambdas.ts, 6, 18))
3838

3939
this.counter++
40-
>this.counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))
41-
>this : Symbol(, Decl(selfInLambdas.ts, 10, 7))
42-
>counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))
43-
4440
var f = () => this.counter;
4541
>f : Symbol(f, Decl(selfInLambdas.ts, 18, 15))
46-
>this.counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))
47-
>this : Symbol(, Decl(selfInLambdas.ts, 10, 7))
48-
>counter : Symbol(counter, Decl(selfInLambdas.ts, 10, 9))
4942

5043
}
5144

0 commit comments

Comments
 (0)