Skip to content

Commit c12cb83

Browse files
committed
Type 'this' in object literal function properties
Previously, methods of object literals would give a type to 'this'. Now function properties of object literals also give a type to 'this'.
1 parent 9120121 commit c12cb83

20 files changed

+158
-87
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8206,10 +8206,11 @@ namespace ts {
82068206
if (signature.thisType) {
82078207
return signature.thisType;
82088208
}
8209-
if (container.parent && container.parent.kind === SyntaxKind.ObjectLiteralExpression) {
8209+
const parentObject = container.parent && container.parent.kind === SyntaxKind.PropertyAssignment ? container.parent.parent : container.parent;
8210+
if (parentObject && parentObject.kind === SyntaxKind.ObjectLiteralExpression) {
82108211
// Note: this works because object literal methods are deferred,
82118212
// which means that the type of the containing object literal is already known.
8212-
const type = checkExpressionCached(<ObjectLiteralExpression>container.parent);
8213+
const type = checkExpressionCached(<ObjectLiteralExpression>parentObject);
82138214
if (type) {
82148215
return type;
82158216
}

tests/baselines/reference/commentsOnObjectLiteral2.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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; }'.
23

34

4-
==== tests/cases/compiler/commentsOnObjectLiteral2.ts (1 errors) ====
5+
==== tests/cases/compiler/commentsOnObjectLiteral2.ts (2 errors) ====
56
var Person = makeClass(
67
~~~~~~~~~
78
!!! error TS2304: Cannot find name 'makeClass'.
@@ -13,6 +14,8 @@ tests/cases/compiler/commentsOnObjectLiteral2.ts(1,14): error TS2304: Cannot fin
1314
*/
1415
initialize: function(name) {
1516
this.name = name;
17+
~~~~
18+
!!! error TS2339: Property 'name' does not exist on type '{ initialize: (name: any) => void; }'.
1619
} /* trailing comment 1*/,
1720
}
1821
);

tests/baselines/reference/fatarrowfunctions.symbols

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ 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, --, --))
166171
}
167172
};
168173

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() : any
238-
>this.message.toString : any
239-
>this.message : any
240-
>this : any
241-
>message : any
242-
>toString : any
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
243243
>3000 : number
244244
}
245245
};

tests/baselines/reference/fatarrowfunctionsInFunctions.symbols

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

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

2021
setTimeout(function() {
2122
>setTimeout : Symbol(setTimeout, Decl(fatarrowfunctionsInFunctions.ts, 0, 0))
2223

2324
_self.message.toString();
25+
>_self.message.toString : Symbol(String.toString, Decl(lib.d.ts, --, --))
26+
>_self.message : Symbol(message, Decl(fatarrowfunctionsInFunctions.ts, 2, 17))
2427
>_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, --, --))
2530

2631
}, 3000);
2732
}

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 : any
22-
>this : any
21+
>_self : { message: string; start: () => void; }
22+
>this : { message: string; start: () => void; }
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() : any
31-
>_self.message.toString : any
32-
>_self.message : any
33-
>_self : any
34-
>message : any
35-
>toString : any
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
3636

3737
}, 3000);
3838
>3000 : number

tests/baselines/reference/looseThisTypeInFunctions.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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'.
45
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(33,28): error TS2339: Property 'length' does not exist on type 'number'.
56
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'.
67
tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(46,20): error TS2339: Property 'length' does not exist on type 'number'.
78

89

9-
==== tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts (4 errors) ====
10+
==== tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts (5 errors) ====
1011
interface I {
1112
n: number;
1213
explicitThis(this: this, m: number): number;
@@ -36,6 +37,8 @@ tests/cases/conformance/types/thisType/looseThisTypeInFunctions.ts(46,20): error
3637
n: 101,
3738
explicitThis: function (m: number) {
3839
return m + this.n.length; // ok, this.n: any
40+
~~~~~~
41+
!!! error TS2339: Property 'length' does not exist on type 'number'.
3942
},
4043
implicitThis(m: number): number { return m; }
4144
};

tests/baselines/reference/selfInLambdas.symbols

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ 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+
4044
var f = () => this.counter;
4145
>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))
4249

4350
}
4451

tests/baselines/reference/selfInLambdas.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ var o = {
4343

4444
this.counter++
4545
>this.counter++ : number
46-
>this.counter : any
47-
>this : any
48-
>counter : any
46+
>this.counter : number
47+
>this : { counter: number; start: () => void; }
48+
>counter : number
4949

5050
var f = () => this.counter;
51-
>f : () => any
52-
>() => this.counter : () => any
53-
>this.counter : any
54-
>this : any
55-
>counter : any
51+
>f : () => number
52+
>() => this.counter : () => number
53+
>this.counter : number
54+
>this : { counter: number; start: () => void; }
55+
>counter : number
5656

5757
}
5858

tests/baselines/reference/thisBinding2.symbols

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ var messenger = {
5050
return setTimeout(() => { var x = this.message; }, 3000);
5151
>setTimeout : Symbol(setTimeout, Decl(thisBinding2.ts, 12, 1))
5252
>x : Symbol(x, Decl(thisBinding2.ts, 17, 37))
53+
>this.message : Symbol(message, Decl(thisBinding2.ts, 14, 17))
54+
>this : Symbol(, Decl(thisBinding2.ts, 14, 15))
55+
>message : Symbol(message, Decl(thisBinding2.ts, 14, 17))
5356
}
5457
};
5558

tests/baselines/reference/thisBinding2.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ var messenger = {
6767
>setTimeout(() => { var x = this.message; }, 3000) : number
6868
>setTimeout : (expression: any, msec?: number, language?: any) => number
6969
>() => { var x = this.message; } : () => void
70-
>x : any
71-
>this.message : any
72-
>this : any
73-
>message : any
70+
>x : string
71+
>this.message : string
72+
>this : { message: string; start: () => number; }
73+
>message : string
7474
>3000 : number
7575
}
7676
};

tests/baselines/reference/thisInPropertyBoundDeclarations.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class A {
7070

7171
a: function() { return this; },
7272
>a : Symbol(a, Decl(thisInPropertyBoundDeclarations.ts, 33, 13))
73+
>this : Symbol(, Decl(thisInPropertyBoundDeclarations.ts, 33, 11))
7374

7475
};
7576

@@ -79,6 +80,7 @@ class A {
7980
return {
8081
a: function() { return this; },
8182
>a : Symbol(a, Decl(thisInPropertyBoundDeclarations.ts, 38, 16))
83+
>this : Symbol(, Decl(thisInPropertyBoundDeclarations.ts, 38, 14))
8284

8385
};
8486
};

tests/baselines/reference/thisInPropertyBoundDeclarations.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ class A {
8484
>{ a: function() { return this; }, } : { a: () => any; }
8585

8686
a: function() { return this; },
87-
>a : () => any
88-
>function() { return this; } : () => any
89-
>this : any
87+
>a : () => { a: any; }
88+
>function() { return this; } : () => { a: any; }
89+
>this : { a: () => any; }
9090

9191
};
9292

@@ -98,9 +98,9 @@ class A {
9898
>{ a: function() { return this; }, } : { a: () => any; }
9999

100100
a: function() { return this; },
101-
>a : () => any
102-
>function() { return this; } : () => any
103-
>this : any
101+
>a : () => { a: any; }
102+
>function() { return this; } : () => { a: any; }
103+
>this : { a: () => any; }
104104

105105
};
106106
};

tests/baselines/reference/thisTypeInObjectLiterals.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ let o = {
33
d: "bar",
44
m() {
55
return this.d.length;
6+
},
7+
f: function() {
8+
return this.d.length;
69
}
710
}
11+
812
let mutuallyRecursive = {
913
a: 100,
1014
start() {
@@ -35,6 +39,9 @@ var o = {
3539
d: "bar",
3640
m: function () {
3741
return this.d.length;
42+
},
43+
f: function () {
44+
return this.d.length;
3845
}
3946
};
4047
var mutuallyRecursive = {

tests/baselines/reference/thisTypeInObjectLiterals.symbols

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,80 +13,92 @@ let o = {
1313
>this.d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
1414
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 0, 7))
1515
>d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
16+
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
17+
18+
},
19+
f: function() {
20+
>f : Symbol(f, Decl(thisTypeInObjectLiterals.ts, 4, 6))
21+
22+
return this.d.length;
23+
>this.d.length : Symbol(String.length, Decl(lib.d.ts, --, --))
24+
>this.d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
25+
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 0, 7))
26+
>d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
1627
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
1728
}
1829
}
30+
1931
let mutuallyRecursive = {
20-
>mutuallyRecursive : Symbol(mutuallyRecursive, Decl(thisTypeInObjectLiterals.ts, 6, 3))
32+
>mutuallyRecursive : Symbol(mutuallyRecursive, Decl(thisTypeInObjectLiterals.ts, 10, 3))
2133

2234
a: 100,
23-
>a : Symbol(a, Decl(thisTypeInObjectLiterals.ts, 6, 25))
35+
>a : Symbol(a, Decl(thisTypeInObjectLiterals.ts, 10, 25))
2436

2537
start() {
26-
>start : Symbol(start, Decl(thisTypeInObjectLiterals.ts, 7, 11))
38+
>start : Symbol(start, Decl(thisTypeInObjectLiterals.ts, 11, 11))
2739

2840
return this.passthrough(this.a);
29-
>this.passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 10, 6))
30-
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 6, 23))
31-
>passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 10, 6))
32-
>this.a : Symbol(a, Decl(thisTypeInObjectLiterals.ts, 6, 25))
33-
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 6, 23))
34-
>a : Symbol(a, Decl(thisTypeInObjectLiterals.ts, 6, 25))
41+
>this.passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 14, 6))
42+
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 10, 23))
43+
>passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 14, 6))
44+
>this.a : Symbol(a, Decl(thisTypeInObjectLiterals.ts, 10, 25))
45+
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 10, 23))
46+
>a : Symbol(a, Decl(thisTypeInObjectLiterals.ts, 10, 25))
3547

3648
},
3749
passthrough(n: number) {
38-
>passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 10, 6))
39-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 11, 16))
50+
>passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 14, 6))
51+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 15, 16))
4052

4153
return this.sub1(n);
42-
>this.sub1 : Symbol(sub1, Decl(thisTypeInObjectLiterals.ts, 13, 6))
43-
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 6, 23))
44-
>sub1 : Symbol(sub1, Decl(thisTypeInObjectLiterals.ts, 13, 6))
45-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 11, 16))
54+
>this.sub1 : Symbol(sub1, Decl(thisTypeInObjectLiterals.ts, 17, 6))
55+
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 10, 23))
56+
>sub1 : Symbol(sub1, Decl(thisTypeInObjectLiterals.ts, 17, 6))
57+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 15, 16))
4658

4759
},
4860
sub1(n: number): number {
49-
>sub1 : Symbol(sub1, Decl(thisTypeInObjectLiterals.ts, 13, 6))
50-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 14, 9))
61+
>sub1 : Symbol(sub1, Decl(thisTypeInObjectLiterals.ts, 17, 6))
62+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 18, 9))
5163

5264
if (n > 0) {
53-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 14, 9))
65+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 18, 9))
5466

5567
return this.passthrough(n - 1);
56-
>this.passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 10, 6))
57-
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 6, 23))
58-
>passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 10, 6))
59-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 14, 9))
68+
>this.passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 14, 6))
69+
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 10, 23))
70+
>passthrough : Symbol(passthrough, Decl(thisTypeInObjectLiterals.ts, 14, 6))
71+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 18, 9))
6072
}
6173
return n;
62-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 14, 9))
74+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 18, 9))
6375
}
6476
}
6577
var i: number = mutuallyRecursive.start();
66-
>i : Symbol(i, Decl(thisTypeInObjectLiterals.ts, 21, 3))
67-
>mutuallyRecursive.start : Symbol(start, Decl(thisTypeInObjectLiterals.ts, 7, 11))
68-
>mutuallyRecursive : Symbol(mutuallyRecursive, Decl(thisTypeInObjectLiterals.ts, 6, 3))
69-
>start : Symbol(start, Decl(thisTypeInObjectLiterals.ts, 7, 11))
78+
>i : Symbol(i, Decl(thisTypeInObjectLiterals.ts, 25, 3))
79+
>mutuallyRecursive.start : Symbol(start, Decl(thisTypeInObjectLiterals.ts, 11, 11))
80+
>mutuallyRecursive : Symbol(mutuallyRecursive, Decl(thisTypeInObjectLiterals.ts, 10, 3))
81+
>start : Symbol(start, Decl(thisTypeInObjectLiterals.ts, 11, 11))
7082

7183
interface I {
72-
>I : Symbol(I, Decl(thisTypeInObjectLiterals.ts, 21, 42))
84+
>I : Symbol(I, Decl(thisTypeInObjectLiterals.ts, 25, 42))
7385

7486
a: number;
75-
>a : Symbol(I.a, Decl(thisTypeInObjectLiterals.ts, 22, 13))
87+
>a : Symbol(I.a, Decl(thisTypeInObjectLiterals.ts, 26, 13))
7688

7789
start(): number;
78-
>start : Symbol(I.start, Decl(thisTypeInObjectLiterals.ts, 23, 14))
90+
>start : Symbol(I.start, Decl(thisTypeInObjectLiterals.ts, 27, 14))
7991

8092
passthrough(n: number): number;
81-
>passthrough : Symbol(I.passthrough, Decl(thisTypeInObjectLiterals.ts, 24, 20))
82-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 25, 16))
93+
>passthrough : Symbol(I.passthrough, Decl(thisTypeInObjectLiterals.ts, 28, 20))
94+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 29, 16))
8395

8496
sub1(n: number): number;
85-
>sub1 : Symbol(I.sub1, Decl(thisTypeInObjectLiterals.ts, 25, 35))
86-
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 26, 9))
97+
>sub1 : Symbol(I.sub1, Decl(thisTypeInObjectLiterals.ts, 29, 35))
98+
>n : Symbol(n, Decl(thisTypeInObjectLiterals.ts, 30, 9))
8799
}
88100
var impl: I = mutuallyRecursive;
89-
>impl : Symbol(impl, Decl(thisTypeInObjectLiterals.ts, 28, 3))
90-
>I : Symbol(I, Decl(thisTypeInObjectLiterals.ts, 21, 42))
91-
>mutuallyRecursive : Symbol(mutuallyRecursive, Decl(thisTypeInObjectLiterals.ts, 6, 3))
101+
>impl : Symbol(impl, Decl(thisTypeInObjectLiterals.ts, 32, 3))
102+
>I : Symbol(I, Decl(thisTypeInObjectLiterals.ts, 25, 42))
103+
>mutuallyRecursive : Symbol(mutuallyRecursive, Decl(thisTypeInObjectLiterals.ts, 10, 3))
92104

0 commit comments

Comments
 (0)