Skip to content

Commit 3e0639a

Browse files
authored
Merge pull request #29053 from Microsoft/fixDestructuringControlFlow
Fix destructuring control flow analysis
2 parents d23effc + 2e6366f commit 3e0639a

35 files changed

+759
-303
lines changed

src/compiler/checker.ts

Lines changed: 170 additions & 187 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,14 +1644,6 @@
16441644
"category": "Error",
16451645
"code": 2458
16461646
},
1647-
"Type '{0}' has no property '{1}' and no string index signature.": {
1648-
"category": "Error",
1649-
"code": 2459
1650-
},
1651-
"Type '{0}' has no property '{1}'.": {
1652-
"category": "Error",
1653-
"code": 2460
1654-
},
16551647
"Type '{0}' is not an array type.": {
16561648
"category": "Error",
16571649
"code": 2461
@@ -1768,7 +1760,7 @@
17681760
"category": "Error",
17691761
"code": 2492
17701762
},
1771-
"Tuple type '{0}' with length '{1}' cannot be assigned to tuple with length '{2}'.": {
1763+
"Tuple type '{0}' of length '{1}' has no element at index '{2}'.": {
17721764
"category": "Error",
17731765
"code": 2493
17741766
},

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,9 @@ namespace ts {
17451745
export type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression;
17461746
export type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression;
17471747

1748+
/* @internal */
1749+
export type AccessExpression = PropertyAccessExpression | ElementAccessExpression;
1750+
17481751
export interface PropertyAccessExpression extends MemberExpression, NamedDeclaration {
17491752
kind: SyntaxKind.PropertyAccessExpression;
17501753
expression: LeftHandSideExpression;

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4590,6 +4590,10 @@ namespace ts {
45904590
|| kind === SyntaxKind.JSDocFunctionType
45914591
|| kind === SyntaxKind.JSDocVariadicType;
45924592
}
4593+
4594+
export function isAccessExpression(node: Node): node is AccessExpression {
4595+
return node.kind === SyntaxKind.PropertyAccessExpression || node.kind === SyntaxKind.ElementAccessExpression;
4596+
}
45934597
}
45944598

45954599
namespace ts {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,8): error TS2459: Type 'undefined' has no property 'a' and no string index signature.
2-
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,18): error TS2459: Type 'undefined' has no property 'b' and no string index signature.
1+
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,8): error TS2339: Property 'a' does not exist on type 'undefined'.
2+
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,18): error TS2339: Property 'b' does not exist on type 'undefined'.
33

44

55
==== tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts (2 errors) ====
66
var a: string, b: number;
77

88
for ({ a: b = 1, b: a = ""} of []) {
99
~
10-
!!! error TS2459: Type 'undefined' has no property 'a' and no string index signature.
10+
!!! error TS2339: Property 'a' does not exist on type 'undefined'.
1111
~
12-
!!! error TS2459: Type 'undefined' has no property 'b' and no string index signature.
12+
!!! error TS2339: Property 'b' does not exist on type 'undefined'.
1313
a;
1414
b;
1515
}

tests/baselines/reference/arityAndOrderCompatibility01.errors.txt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): error TS2493: Tuple type '[string, number]' with length '2' cannot be assigned to tuple with length '3'.
2-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(16,12): error TS2460: Type 'StrNum' has no property '2'.
1+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): error TS2493: Tuple type '[string, number]' of length '2' has no element at index '2'.
32
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,5): error TS2461: Type '{ 0: string; 1: number; length: 2; }' is not an array type.
4-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,12): error TS2460: Type '{ 0: string; 1: number; length: 2; }' has no property '2'.
53
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2741: Property '2' is missing in type '[string, number]' but required in type '[number, number, number]'.
64
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2741: Property '2' is missing in type 'StrNum' but required in type '[number, number, number]'.
75
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, number, number]': 2, pop, push, concat, and 16 more.
@@ -30,7 +28,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(31,5): error
3028
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, string]': pop, push, concat, join, and 15 more.
3129

3230

33-
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (19 errors) ====
31+
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (17 errors) ====
3432
interface StrNum extends Array<string|number> {
3533
0: string;
3634
1: number;
@@ -47,15 +45,11 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error
4745

4846
var [a, b, c] = x;
4947
~
50-
!!! error TS2493: Tuple type '[string, number]' with length '2' cannot be assigned to tuple with length '3'.
48+
!!! error TS2493: Tuple type '[string, number]' of length '2' has no element at index '2'.
5149
var [d, e, f] = y;
52-
~
53-
!!! error TS2460: Type 'StrNum' has no property '2'.
5450
var [g, h, i] = z;
5551
~~~~~~~~~
5652
!!! error TS2461: Type '{ 0: string; 1: number; length: 2; }' is not an array type.
57-
~
58-
!!! error TS2460: Type '{ 0: string; 1: number; length: 2; }' has no property '2'.
5953
var j1: [number, number, number] = x;
6054
~~
6155
!!! error TS2741: Property '2' is missing in type '[string, number]' but required in type '[number, number, number]'.

tests/baselines/reference/arityAndOrderCompatibility01.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ var z: {
3232
var [a, b, c] = x;
3333
>a : string
3434
>b : number
35-
>c : any
35+
>c : undefined
3636
>x : [string, number]
3737

3838
var [d, e, f] = y;
3939
>d : string
4040
>e : number
41-
>f : any
41+
>f : string | number
4242
>y : StrNum
4343

4444
var [g, h, i] = z;
45-
>g : string
46-
>h : number
45+
>g : any
46+
>h : any
4747
>i : any
4848
>z : { 0: string; 1: number; length: 2; }
4949

tests/baselines/reference/bestCommonTypeOfTuple.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(22,13): error TS2339: Property '2' does not exist on type '[(x: number) => string, (x: number) => number]'.
2-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(23,13): error TS2339: Property '2' does not exist on type '[E1, E2]'.
3-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(24,13): error TS2339: Property '2' does not exist on type '[number, any]'.
4-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(25,13): error TS2339: Property '3' does not exist on type '[E1, E2, number]'.
1+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(22,13): error TS2493: Tuple type '[(x: number) => string, (x: number) => number]' of length '2' has no element at index '2'.
2+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(23,13): error TS2493: Tuple type '[E1, E2]' of length '2' has no element at index '2'.
3+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(24,13): error TS2493: Tuple type '[number, any]' of length '2' has no element at index '2'.
4+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(25,13): error TS2493: Tuple type '[E1, E2, number]' of length '3' has no element at index '3'.
55

66

77
==== tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts (4 errors) ====
@@ -28,13 +28,13 @@ tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfT
2828
t4 = [E1.one, E2.two, 20];
2929
var e1 = t1[2]; // {}
3030
~
31-
!!! error TS2339: Property '2' does not exist on type '[(x: number) => string, (x: number) => number]'.
31+
!!! error TS2493: Tuple type '[(x: number) => string, (x: number) => number]' of length '2' has no element at index '2'.
3232
var e2 = t2[2]; // {}
3333
~
34-
!!! error TS2339: Property '2' does not exist on type '[E1, E2]'.
34+
!!! error TS2493: Tuple type '[E1, E2]' of length '2' has no element at index '2'.
3535
var e3 = t3[2]; // any
3636
~
37-
!!! error TS2339: Property '2' does not exist on type '[number, any]'.
37+
!!! error TS2493: Tuple type '[number, any]' of length '2' has no element at index '2'.
3838
var e4 = t4[3]; // number
3939
~
40-
!!! error TS2339: Property '3' does not exist on type '[E1, E2, number]'.
40+
!!! error TS2493: Tuple type '[E1, E2, number]' of length '3' has no element at index '3'.

tests/baselines/reference/bestCommonTypeOfTuple2.errors.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(17,14): error TS2339: Property '4' does not exist on type '[C, base]'.
2-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(18,14): error TS2339: Property '4' does not exist on type '[C, D]'.
3-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(19,14): error TS2339: Property '4' does not exist on type '[C1, D1]'.
4-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(20,14): error TS2339: Property '2' does not exist on type '[base1, C1]'.
5-
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(21,14): error TS2339: Property '2' does not exist on type '[C1, F]'.
1+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(17,14): error TS2493: Tuple type '[C, base]' of length '2' has no element at index '4'.
2+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(18,14): error TS2493: Tuple type '[C, D]' of length '2' has no element at index '4'.
3+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(19,14): error TS2493: Tuple type '[C1, D1]' of length '2' has no element at index '4'.
4+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(20,14): error TS2493: Tuple type '[base1, C1]' of length '2' has no element at index '2'.
5+
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(21,14): error TS2493: Tuple type '[C1, F]' of length '2' has no element at index '2'.
66

77

88
==== tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts (5 errors) ====
@@ -24,17 +24,17 @@ tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfT
2424

2525
var e11 = t1[4]; // base
2626
~
27-
!!! error TS2339: Property '4' does not exist on type '[C, base]'.
27+
!!! error TS2493: Tuple type '[C, base]' of length '2' has no element at index '4'.
2828
var e21 = t2[4]; // {}
2929
~
30-
!!! error TS2339: Property '4' does not exist on type '[C, D]'.
30+
!!! error TS2493: Tuple type '[C, D]' of length '2' has no element at index '4'.
3131
var e31 = t3[4]; // C1
3232
~
33-
!!! error TS2339: Property '4' does not exist on type '[C1, D1]'.
33+
!!! error TS2493: Tuple type '[C1, D1]' of length '2' has no element at index '4'.
3434
var e41 = t4[2]; // base1
3535
~
36-
!!! error TS2339: Property '2' does not exist on type '[base1, C1]'.
36+
!!! error TS2493: Tuple type '[base1, C1]' of length '2' has no element at index '2'.
3737
var e51 = t5[2]; // {}
3838
~
39-
!!! error TS2339: Property '2' does not exist on type '[C1, F]'.
39+
!!! error TS2493: Tuple type '[C1, F]' of length '2' has no element at index '2'.
4040

tests/baselines/reference/castingTuple.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tests/cases/conformance/types/tuple/castingTuple.ts(14,15): error TS2352: Conver
66
tests/cases/conformance/types/tuple/castingTuple.ts(15,14): error TS2352: Conversion of type '[number, string]' to type '[number, string, boolean]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
77
tests/cases/conformance/types/tuple/castingTuple.ts(18,21): error TS2352: Conversion of type '[C, D]' to type '[C, D, A]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
88
Property '2' is missing in type '[C, D]' but required in type '[C, D, A]'.
9-
tests/cases/conformance/types/tuple/castingTuple.ts(20,33): error TS2339: Property '5' does not exist on type '[C, D, A]'.
9+
tests/cases/conformance/types/tuple/castingTuple.ts(20,33): error TS2493: Tuple type '[C, D, A]' of length '3' has no element at index '5'.
1010
tests/cases/conformance/types/tuple/castingTuple.ts(30,10): error TS2352: Conversion of type '[number, string]' to type '[number, number]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
1111
Type 'string' is not comparable to type 'number'.
1212
tests/cases/conformance/types/tuple/castingTuple.ts(31,10): error TS2352: Conversion of type '[C, D]' to type '[A, I]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
@@ -49,7 +49,7 @@ tests/cases/conformance/types/tuple/castingTuple.ts(33,1): error TS2304: Cannot
4949
var eleFromCDA1 = classCDATuple[2]; // A
5050
var eleFromCDA2 = classCDATuple[5]; // C | D | A
5151
~
52-
!!! error TS2339: Property '5' does not exist on type '[C, D, A]'.
52+
!!! error TS2493: Tuple type '[C, D, A]' of length '3' has no element at index '5'.
5353
var t10: [E1, E2] = [E1.one, E2.one];
5454
var t11 = <[number, number]>t10;
5555
var array1 = <{}[]>emptyObjTuple;

0 commit comments

Comments
 (0)