Support contextual type for property assignments in JS that are not declarations (#18820)

This commit is contained in:
Andy 2017-10-04 09:32:09 -07:00 committed by GitHub
parent a3853d0774
commit fe9129b1ab
3 changed files with 29 additions and 7 deletions

View File

@ -13258,13 +13258,25 @@ namespace ts {
const binaryExpression = <BinaryExpression>node.parent;
const operator = binaryExpression.operatorToken.kind;
if (isAssignmentOperator(operator)) {
// Don't do this for special property assignments to avoid circularity
if (getSpecialPropertyAssignmentKind(binaryExpression) !== SpecialPropertyAssignmentKind.None) {
return undefined;
}
// In an assignment expression, the right operand is contextually typed by the type of the left operand.
if (node === binaryExpression.right) {
// Don't do this for special property assignments to avoid circularity
switch (getSpecialPropertyAssignmentKind(binaryExpression)) {
case SpecialPropertyAssignmentKind.None:
break;
case SpecialPropertyAssignmentKind.Property:
// If `binaryExpression.left` was assigned a symbol, then this is a new declaration; otherwise it is an assignment to an existing declaration.
// See `bindStaticPropertyAssignment` in `binder.ts`.
if (!binaryExpression.left.symbol) {
break;
}
// falls through
case SpecialPropertyAssignmentKind.ExportsProperty:
case SpecialPropertyAssignmentKind.ModuleExports:
case SpecialPropertyAssignmentKind.PrototypeProperty:
case SpecialPropertyAssignmentKind.ThisProperty:
return undefined;
}
// In an assignment expression, the right operand is contextually typed by the type of the left operand.
return getTypeOfExpression(binaryExpression.left);
}
}

View File

@ -23,7 +23,7 @@ verify.getAndApplyCodeFix(/*errorCode*/undefined, 0);
verify.getAndApplyCodeFix(/*errorCode*/undefined, 0);
verify.rangeIs(`
y: { [x: string]: any; };
y: {};
m1(): any {
throw new Error("Method not implemented.");
}

View File

@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />
// @allowJs: true
// @Filename: /a.js
/////** @type {{ p: "x" | "y" }} */
////const x = { p: "x" };
////x.p = "/**/";
verify.completionsAt("", ["x", "y"]);