Check this expressions in object literal methods

Add a test and baseline
This commit is contained in:
Nathan Shively-Sanders 2016-03-09 13:06:06 -08:00
parent 482acccada
commit 7b531fcd05
5 changed files with 70 additions and 0 deletions

View File

@ -7494,6 +7494,14 @@ namespace ts {
const symbol = getSymbolOfNode(container.parent);
return container.flags & NodeFlags.Static ? getTypeOfSymbol(symbol) : (<InterfaceType>getDeclaredTypeOfSymbol(symbol)).thisType;
}
if (container.parent && container.parent.kind === SyntaxKind.ObjectLiteralExpression) {
// Note: this works because object literal methods are deferred,
// which means that the type of the containing object literal is already known.
const type = checkExpressionCached(<ObjectLiteralExpression>container.parent);
if (type) {
return type;
}
}
if (isInJavaScriptFile(node)) {
const type = getTypeForThisExpressionFromJSDoc(container);

View File

@ -0,0 +1,16 @@
//// [thisTypeInObjectLiterals.ts]
let o = {
d: "bar",
m() {
return this.d.length;
}
}
//// [thisTypeInObjectLiterals.js]
var o = {
d: "bar",
m: function () {
return this.d.length;
}
};

View File

@ -0,0 +1,19 @@
=== tests/cases/conformance/types/thisType/thisTypeInObjectLiterals.ts ===
let o = {
>o : Symbol(o, Decl(thisTypeInObjectLiterals.ts, 0, 3))
d: "bar",
>d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
m() {
>m : Symbol(m, Decl(thisTypeInObjectLiterals.ts, 1, 13))
return this.d.length;
>this.d.length : Symbol(String.length, Decl(lib.d.ts, --, --))
>this.d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
>this : Symbol(, Decl(thisTypeInObjectLiterals.ts, 0, 7))
>d : Symbol(d, Decl(thisTypeInObjectLiterals.ts, 0, 9))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
}
}

View File

@ -0,0 +1,21 @@
=== tests/cases/conformance/types/thisType/thisTypeInObjectLiterals.ts ===
let o = {
>o : { d: string; m(): number; }
>{ d: "bar", m() { return this.d.length; }} : { d: string; m(): number; }
d: "bar",
>d : string
>"bar" : string
m() {
>m : () => number
return this.d.length;
>this.d.length : number
>this.d : string
>this : { d: string; m(): number; }
>d : string
>length : number
}
}

View File

@ -0,0 +1,6 @@
let o = {
d: "bar",
m() {
return this.d.length;
}
}