From f6a3a3fc3dc5aac5d5b1bbec8a64656be6cbda85 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 14 Feb 2017 12:04:39 -0800 Subject: [PATCH] Use '__this__' property in contextual type to indicate type of 'this' --- src/compiler/checker.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2b059e29d09..1f59d28cc7c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11189,6 +11189,19 @@ namespace ts { return getTypeOfSymbol(thisParameter); } } + if (isObjectLiteralMethod(func)) { + // For methods in an object literal, look for a '__this__' property in the contextual + // type for the object literal. If one exists, remove 'undefined' from the type of that + // property and report it as the contextual type for 'this' in the method. + const objectLiteral = func.parent; + const type = getApparentTypeOfContextualType(objectLiteral); + if (type) { + const propertyType = getTypeOfPropertyOfContextualType(type, "___this__"); + if (propertyType) { + return getNonNullableType(propertyType); + } + } + } } return undefined; }