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; }