Skip to content

Commit 270c0b8

Browse files
authored
Merge pull request microsoft#13959 from Microsoft/fix-assigned-type-of-assignment-nested-in-literals
Fix assigned type of assignment nested in literals
2 parents f7b2062 + a46cb03 commit 270c0b8

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9472,11 +9472,19 @@ namespace ts {
94729472
}
94739473

94749474
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
9475-
return node.parent.kind === SyntaxKind.ArrayLiteralExpression || node.parent.kind === SyntaxKind.PropertyAssignment ?
9475+
const isDestructuringDefaultAssignment =
9476+
node.parent.kind === SyntaxKind.ArrayLiteralExpression && isDestructuringAssignmentTarget(node.parent) ||
9477+
node.parent.kind === SyntaxKind.PropertyAssignment && isDestructuringAssignmentTarget(node.parent.parent);
9478+
return isDestructuringDefaultAssignment ?
94769479
getTypeWithDefault(getAssignedType(node), node.right) :
94779480
getTypeOfExpression(node.right);
94789481
}
94799482

9483+
function isDestructuringAssignmentTarget(parent: Node) {
9484+
return parent.parent.kind === SyntaxKind.BinaryExpression && (parent.parent as BinaryExpression).left === parent ||
9485+
parent.parent.kind === SyntaxKind.ForOfStatement && (parent.parent as ForOfStatement).initializer === parent;
9486+
}
9487+
94809488
function getAssignedTypeOfArrayLiteralElement(node: ArrayLiteralExpression, element: Expression): Type {
94819489
return getTypeOfDestructuredArrayElement(getAssignedType(node), indexOf(node.elements, element));
94829490
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
var kowloona, c, d;
9+
for (kowloona of [c = 1, d = c]) {
10+
}
11+
12+
13+
//// [assignmentNestedInLiterals.js]
14+
var target, x, y;
15+
target = [x = 1, y = x];
16+
var aegis, a, b;
17+
aegis = { x: a = 1, y: b = a };
18+
var kowloona, c, d;
19+
for (var _i = 0, _a = [c = 1, d = c]; _i < _a.length; _i++) {
20+
kowloona = _a[_i];
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
26+
var kowloona, c, d;
27+
>kowloona : Symbol(kowloona, Decl(assignmentNestedInLiterals.ts, 6, 3))
28+
>c : Symbol(c, Decl(assignmentNestedInLiterals.ts, 6, 13))
29+
>d : Symbol(d, Decl(assignmentNestedInLiterals.ts, 6, 16))
30+
31+
for (kowloona of [c = 1, d = c]) {
32+
>kowloona : Symbol(kowloona, Decl(assignmentNestedInLiterals.ts, 6, 3))
33+
>c : Symbol(c, Decl(assignmentNestedInLiterals.ts, 6, 13))
34+
>d : Symbol(d, Decl(assignmentNestedInLiterals.ts, 6, 16))
35+
>c : Symbol(c, Decl(assignmentNestedInLiterals.ts, 6, 13))
36+
}
37+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
36+
var kowloona, c, d;
37+
>kowloona : any
38+
>c : any
39+
>d : any
40+
41+
for (kowloona of [c = 1, d = c]) {
42+
>kowloona : any
43+
>[c = 1, d = c] : number[]
44+
>c = 1 : 1
45+
>c : any
46+
>1 : 1
47+
>d = c : number
48+
>d : any
49+
>c : number
50+
}
51+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 };
7+
8+
var kowloona, c, d;
9+
for (kowloona of [c = 1, d = c]) {
10+
}

0 commit comments

Comments
 (0)