Skip to content

Commit 0a2ba0c

Browse files
committed
Make this in object literal always of type any
Previously, `this` was implicitly typed by the shape of its containing object literal. This is not correct for JavaScript-style inheritance uses of object literals, and the previous fix was not correct either. So we're going back to `this: any` in object literals for now.
1 parent 798257c commit 0a2ba0c

30 files changed

+102
-206
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8206,15 +8206,6 @@ namespace ts {
82068206
if (signature.thisType) {
82078207
return signature.thisType;
82088208
}
8209-
const parentObject = container.parent && container.parent.kind === SyntaxKind.PropertyAssignment ? container.parent.parent : container.parent;
8210-
if (parentObject && parentObject.kind === SyntaxKind.ObjectLiteralExpression) {
8211-
// Note: this works because object literal methods are deferred,
8212-
// which means that the type of the containing object literal is already known.
8213-
const type = checkExpressionCached(<ObjectLiteralExpression>parentObject);
8214-
if (type) {
8215-
return type;
8216-
}
8217-
}
82188209
}
82198210
if (isClassLike(container.parent)) {
82208211
const symbol = getSymbolOfNode(container.parent);
@@ -8454,10 +8445,7 @@ namespace ts {
84548445
if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== SyntaxKind.ArrowFunction) {
84558446
const contextualSignature = getContextualSignature(func);
84568447
if (contextualSignature) {
8457-
return contextualSignature.thisType || anyType;
8458-
}
8459-
else if (getContextualTypeForFunctionLikeDeclaration(func) === anyType) {
8460-
return anyType;
8448+
return contextualSignature.thisType;
84618449
}
84628450
}
84638451

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
}

0 commit comments

Comments
 (0)