mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:33:53 -06:00
Support contextual type for property assignments in JS that are not declarations (#18820)
This commit is contained in:
parent
a3853d0774
commit
fe9129b1ab
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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.");
|
||||
}
|
||||
|
||||
10
tests/cases/fourslash/completionsJsPropertyAssignment.ts
Normal file
10
tests/cases/fourslash/completionsJsPropertyAssignment.ts
Normal 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"]);
|
||||
Loading…
x
Reference in New Issue
Block a user