diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b5337082c0f..7073af85fcf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7494,6 +7494,14 @@ namespace ts { const symbol = getSymbolOfNode(container.parent); return container.flags & NodeFlags.Static ? getTypeOfSymbol(symbol) : (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(container.parent); + if (type) { + return type; + } + } if (isInJavaScriptFile(node)) { const type = getTypeForThisExpressionFromJSDoc(container); diff --git a/tests/baselines/reference/thisTypeInObjectLiterals.js b/tests/baselines/reference/thisTypeInObjectLiterals.js new file mode 100644 index 00000000000..78854bf2388 --- /dev/null +++ b/tests/baselines/reference/thisTypeInObjectLiterals.js @@ -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; + } +}; diff --git a/tests/baselines/reference/thisTypeInObjectLiterals.symbols b/tests/baselines/reference/thisTypeInObjectLiterals.symbols new file mode 100644 index 00000000000..6bea6d88c58 --- /dev/null +++ b/tests/baselines/reference/thisTypeInObjectLiterals.symbols @@ -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, --, --)) + } +} + diff --git a/tests/baselines/reference/thisTypeInObjectLiterals.types b/tests/baselines/reference/thisTypeInObjectLiterals.types new file mode 100644 index 00000000000..824b0b00f4c --- /dev/null +++ b/tests/baselines/reference/thisTypeInObjectLiterals.types @@ -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 + } +} + diff --git a/tests/cases/conformance/types/thisType/thisTypeInObjectLiterals.ts b/tests/cases/conformance/types/thisType/thisTypeInObjectLiterals.ts new file mode 100644 index 00000000000..11c6b58d710 --- /dev/null +++ b/tests/cases/conformance/types/thisType/thisTypeInObjectLiterals.ts @@ -0,0 +1,6 @@ +let o = { + d: "bar", + m() { + return this.d.length; + } +}