Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13096,13 +13096,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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/codeFixUndeclaredAcrossFiles1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/fourslash/completionsJsPropertyAssignment.ts
Original file line number Diff line number Diff line change
@@ -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"]);