Skip to content

Commit fe9129b

Browse files
author
Andy
authored
Support contextual type for property assignments in JS that are not declarations (microsoft#18820)
1 parent a3853d0 commit fe9129b

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13258,13 +13258,25 @@ namespace ts {
1325813258
const binaryExpression = <BinaryExpression>node.parent;
1325913259
const operator = binaryExpression.operatorToken.kind;
1326013260
if (isAssignmentOperator(operator)) {
13261-
// Don't do this for special property assignments to avoid circularity
13262-
if (getSpecialPropertyAssignmentKind(binaryExpression) !== SpecialPropertyAssignmentKind.None) {
13263-
return undefined;
13264-
}
13265-
13266-
// In an assignment expression, the right operand is contextually typed by the type of the left operand.
1326713261
if (node === binaryExpression.right) {
13262+
// Don't do this for special property assignments to avoid circularity
13263+
switch (getSpecialPropertyAssignmentKind(binaryExpression)) {
13264+
case SpecialPropertyAssignmentKind.None:
13265+
break;
13266+
case SpecialPropertyAssignmentKind.Property:
13267+
// If `binaryExpression.left` was assigned a symbol, then this is a new declaration; otherwise it is an assignment to an existing declaration.
13268+
// See `bindStaticPropertyAssignment` in `binder.ts`.
13269+
if (!binaryExpression.left.symbol) {
13270+
break;
13271+
}
13272+
// falls through
13273+
case SpecialPropertyAssignmentKind.ExportsProperty:
13274+
case SpecialPropertyAssignmentKind.ModuleExports:
13275+
case SpecialPropertyAssignmentKind.PrototypeProperty:
13276+
case SpecialPropertyAssignmentKind.ThisProperty:
13277+
return undefined;
13278+
}
13279+
// In an assignment expression, the right operand is contextually typed by the type of the left operand.
1326813280
return getTypeOfExpression(binaryExpression.left);
1326913281
}
1327013282
}

tests/cases/fourslash/codeFixUndeclaredAcrossFiles1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ verify.getAndApplyCodeFix(/*errorCode*/undefined, 0);
2323
verify.getAndApplyCodeFix(/*errorCode*/undefined, 0);
2424

2525
verify.rangeIs(`
26-
y: { [x: string]: any; };
26+
y: {};
2727
m1(): any {
2828
throw new Error("Method not implemented.");
2929
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /a.js
6+
/////** @type {{ p: "x" | "y" }} */
7+
////const x = { p: "x" };
8+
////x.p = "/**/";
9+
10+
verify.completionsAt("", ["x", "y"]);

0 commit comments

Comments
 (0)