From e3a0687327128c4cce55b8ce448ce8ad7d3fca9b Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 17 Feb 2017 06:56:58 -0800 Subject: [PATCH] Contextual this in 'obj.xxx = function(...)' or 'obj[xxx] = function(...)' --- src/compiler/checker.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3208d303ab0..9fb71884c51 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11518,6 +11518,14 @@ namespace ts { // for 'this' is the type of the object literal itself. return checkExpressionCached(containingLiteral); } + // In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the + // contextual type for 'this' is 'obj'. + if (func.parent.kind === SyntaxKind.BinaryExpression && (func.parent).operatorToken.kind === SyntaxKind.EqualsToken) { + const target = (func.parent).left; + if (target.kind === SyntaxKind.PropertyAccessExpression || target.kind === SyntaxKind.ElementAccessExpression) { + return checkExpressionCached((target).expression); + } + } } return undefined; }