Skip to content

Commit 125dd57

Browse files
committed
Fix assigned type of assignment nested in literals
Fixes microsoft#12946
1 parent a7728f8 commit 125dd57

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

src/compiler/checker.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -9469,11 +9469,19 @@ namespace ts {
94699469
}
94709470

94719471
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
9472-
return node.parent.kind === SyntaxKind.ArrayLiteralExpression || node.parent.kind === SyntaxKind.PropertyAssignment ?
9472+
const isDestructuringDefaultAssignment =
9473+
node.parent.kind === SyntaxKind.ArrayLiteralExpression && isDestructuringAssignmentTarget(node.parent) ||
9474+
node.parent.kind === SyntaxKind.PropertyAssignment && isDestructuringAssignmentTarget(node.parent.parent);
9475+
return isDestructuringDefaultAssignment ?
94739476
getTypeWithDefault(getAssignedType(node), node.right) :
94749477
getTypeOfExpression(node.right);
94759478
}
94769479

9480+
function isDestructuringAssignmentTarget(parent: Node) {
9481+
return parent.parent.kind === SyntaxKind.BinaryExpression && (parent.parent as BinaryExpression).left === parent ||
9482+
parent.parent.kind === SyntaxKind.ForOfStatement && (parent.parent as ForOfStatement).initializer === parent;
9483+
}
9484+
94779485
function getAssignedTypeOfArrayLiteralElement(node: ArrayLiteralExpression, element: Expression): Type {
94789486
return getTypeOfDestructuredArrayElement(getAssignedType(node), indexOf(node.elements, element));
94799487
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [assignmentNestedInLiterals.ts]
2+
var target, x, y;
3+
target = [x = 1, y = x];
4+
5+
var aegis, a, b;
6+
aegis = { x: a = 1, y: b = a };
7+
8+
9+
//// [assignmentNestedInLiterals.js]
10+
var target, x, y;
11+
target = [x = 1, y = x];
12+
var aegis, a, b;
13+
aegis = { x: a = 1, y: b = a };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/assignmentNestedInLiterals.ts ===
2+
var target, x, y;
3+
>target : Symbol(target, Decl(assignmentNestedInLiterals.ts, 0, 3))
4+
>x : Symbol(x, Decl(assignmentNestedInLiterals.ts, 0, 11))
5+
>y : Symbol(y, Decl(assignmentNestedInLiterals.ts, 0, 14))
6+
7+
target = [x = 1, y = x];
8+
>target : Symbol(target, Decl(assignmentNestedInLiterals.ts, 0, 3))
9+
>x : Symbol(x, Decl(assignmentNestedInLiterals.ts, 0, 11))
10+
>y : Symbol(y, Decl(assignmentNestedInLiterals.ts, 0, 14))
11+
>x : Symbol(x, Decl(assignmentNestedInLiterals.ts, 0, 11))
12+
13+
var aegis, a, b;
14+
>aegis : Symbol(aegis, Decl(assignmentNestedInLiterals.ts, 3, 3))
15+
>a : Symbol(a, Decl(assignmentNestedInLiterals.ts, 3, 10))
16+
>b : Symbol(b, Decl(assignmentNestedInLiterals.ts, 3, 13))
17+
18+
aegis = { x: a = 1, y: b = a };
19+
>aegis : Symbol(aegis, Decl(assignmentNestedInLiterals.ts, 3, 3))
20+
>x : Symbol(x, Decl(assignmentNestedInLiterals.ts, 4, 9))
21+
>a : Symbol(a, Decl(assignmentNestedInLiterals.ts, 3, 10))
22+
>y : Symbol(y, Decl(assignmentNestedInLiterals.ts, 4, 19))
23+
>b : Symbol(b, Decl(assignmentNestedInLiterals.ts, 3, 13))
24+
>a : Symbol(a, Decl(assignmentNestedInLiterals.ts, 3, 10))
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/assignmentNestedInLiterals.ts ===
2+
var target, x, y;
3+
>target : any
4+
>x : any
5+
>y : any
6+
7+
target = [x = 1, y = x];
8+
>target = [x = 1, y = x] : number[]
9+
>target : any
10+
>[x = 1, y = x] : number[]
11+
>x = 1 : 1
12+
>x : any
13+
>1 : 1
14+
>y = x : number
15+
>y : any
16+
>x : number
17+
18+
var aegis, a, b;
19+
>aegis : any
20+
>a : any
21+
>b : any
22+
23+
aegis = { x: a = 1, y: b = a };
24+
>aegis = { x: a = 1, y: b = a } : { x: number; y: number; }
25+
>aegis : any
26+
>{ x: a = 1, y: b = a } : { x: number; y: number; }
27+
>x : number
28+
>a = 1 : 1
29+
>a : any
30+
>1 : 1
31+
>y : number
32+
>b = a : number
33+
>b : any
34+
>a : number
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @noImplicitAny: true
2+
var target, x, y;
3+
target = [x = 1, y = x];
4+
5+
var aegis, a, b;
6+
aegis = { x: a = 1, y: b = a };

0 commit comments

Comments
 (0)