8000 Merge pull request #2238 from caitp/issue-2090 · johnangularjs/TypeScript@aa08300 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa08300

Browse files
Merge pull request microsoft#2238 from caitp/issue-2090
Improve array destructuring error message
2 parents 11bb5f9 + 0d06729 commit aa08300

File tree

7 files changed

+359
-95
lines changed

7 files changed

+359
-95
lines changed

src/compiler/checker.ts

Lines changed: 107 additions & 89 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ module ts {
338338
The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property: { code: 2490, category: DiagnosticCategory.Error, key: "The type returned by the 'next()' method of an iterator must have a 'value' property." },
339339
The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern: { code: 2491, category: DiagnosticCategory.Error, key: "The left-hand side of a 'for...in' statement cannot be a destructuring pattern." },
340340
Cannot_redeclare_identifier_0_in_catch_clause: { code: 2492, category: DiagnosticCategory.Error, key: "Cannot redeclare identifier '{0}' in catch clause" },
341+
Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2: { code: 2493, category: DiagnosticCategory.Error, key: "Tuple type '{0}' with length '{1}' cannot be assigned to tuple with length '{2}'." },
341342
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
342343
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
343344
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,10 @@
13431343
"category": "Error",
13441344
"code": 2492
13451345
},
1346+
"Tuple type '{0}' with length '{1}' cannot be assigned to tuple with length '{2}'.": {
1347+
"category": "Error",
1348+
"code": 2493
1349+
},
13461350

13471351
"Import declaration '{0}' is using private name '{1}'.": {
13481352
"category": "Error",
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(13,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(14,12): error TS2460: Type 'StrNum' has no property '2'.
3+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,5): error TS2461: Type '{ 0: string; 1: number; }' is not an array type.
4+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(16,5): error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
5+
Types of property '0' are incompatible.
6+
Type 'string' is not assignable to type 'number'.
7+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,5): error TS2322: Type 'StrNum' is not assignable to type '[number, number, number]'.
8+
Types of property '0' are incompatible.
9+
Type 'string' is not assignable to type 'number'.
10+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, number, number]'.
11+
Types of property '0' are incompatible.
12+
Type 'string' is not assignable to type 'number'.
13+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2322: Type '[string, number]' is not assignable to type '[string, number, number]'.
14+
Property '2' is missing in type '[string, number]'.
15+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2322: Type 'StrNum' is not assignable to type '[string, number, number]'.
16+
Property '2' is missing in type 'StrNum'.
17+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(21,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string, number, number]'.
18+
Property '2' is missing in type '{ 0: string; 1: number; }'.
19+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(22,5): error TS2322: Type '[string, number]' is not assignable to type '[number]'.
20+
Types of property '0' are incompatible.
21+
Type 'string' is not assignable to type 'number'.
22+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(23,5): error TS2322: Type 'StrNum' is not assignable to type '[number]'.
23+
Types of property '0' are incompatible.
24+
Type 'string' is not assignable to type 'number'.
25+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(24,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number]'.
26+
Types of property '0' are incompatible.
27+
Type 'string' is not assignable to type 'number'.
28+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(25,5): error TS2322: Type '[string, number]' is not assignable to type '[string]'.
29+
Types of property 'pop' are incompatible.
30+
Type '() => string | number' is not assignable to type '() => string'.
31+
Type 'string | number' is not assignable to type 'string'.
32+
Type 'number' is not assignable to type 'string'.
33+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(26,5): error TS2322: Type 'StrNum' is not assignable to type '[string]'.
34+
Types of property 'pop' are incompatible.
35+
Type '() => string | number' is not assignable to type '() => string'.
36+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(27,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string]'.
37+
Property 'length' is missing in type '{ 0: string; 1: number; }'.
38+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(28,5): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
39+
Types of property '0' are incompatible.
40+
Type 'string' is not assignable to type 'number'.
41+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.
42+
Types of property '0' are incompatible.
43+
Type 'string' is not assignable to type 'number'.
44+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, string]'.
45+
Types of property '0' are incompatible.
46+
Type 'string' is not assignable to type 'number'.
47+
48+
49+
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (18 errors) ====
50+
interface StrNum extends Array<string|number> {
51+
0: string;
52+
1: number;
53+
}
54+
55+
var x: [string, number];
56+
var y: StrNum
57+
var z: {
58+
0: string;
59+
1: number;
60+
}
61+
62+
var [a, b, c] = x;
63+
~
64+
!!! error TS2493: Tuple type '[string, number]' with length '2' cannot be assigned to tuple with length '3'.
65+
var [d, e, f] = y;
66+
~
67+
!!! error TS2460: Type 'StrNum' has no property '2'.
68+
var [g, h, i] = z;
69+
~~~~~~~~~
70+
!!! error TS2461: Type '{ 0: string; 1: number; }' is not an array type.
71+
var j1: [number, number, number] = x;
72+
~~
73+
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
74+
!!! error TS2322: Types of property '0' are incompatible.
75+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
76+
var j2: [number, number, number] = y;
77+
~~
78+
!!! error TS2322: Type 'StrNum' is not assignable to type '[number, number, number]'.
79+
!!! error TS2322: Types of property '0' are incompatible.
80+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
81+
var j3: [number, number, number] = z;
82+
~~
83+
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, number, number]'.
84+
!!! error TS2322: Types of property '0' are incompatible.
85+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
86+
var k1: [string, number, number] = x;
87+
~~
88+
!!! error TS2322: Type '[string, number]' is not assignable to type '[string, number, number]'.
89+
!!! error TS2322: Property '2' is missing in type '[string, number]'.
90+
var k2: [string, number, number] = y;
91+
~~
92+
!!! error TS2322: Type 'StrNum' is not assignable to type '[string, number, number]'.
93+
!!! error TS2322: Property '2' is missing in type 'StrNum'.
94+
var k3: [string, number, number] = z;
95+
~~
96+
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string, number, number]'.
97+
!!! error TS2322: Property '2' is missing in type '{ 0: string; 1: number; }'.
98+
var l1: [number] = x;
99+
~~
100+
!!! error TS2322: Type '[string, number]' is not assignable to type '[number]'.
101+
!!! error TS2322: Types of property '0' are incompatible.
102+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
103+
var l2: [number] = y;
104+
~~
105+
!!! error TS2322: Type 'StrNum' is not assignable to type '[number]'.
106+
!!! error TS2322: Types of property '0' are incompatible.
107+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
108+
var l3: [number] = z;
109+
~~
110+
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number]'.
111+
!!! error TS2322: Types of property '0' are incompatible.
112+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
113+
var m1: [string] = x;
114+
~~
115+
!!! error TS2322: Type '[string, number]' is not assignable to type '[string]'.
116+
!!! error TS2322: Types of property 'pop' are incompatible.
117+
!!! error TS2322: Type '() => string | number' is not assignable to type '() => string'.
118+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
119+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
120+
var m2: [string] = y;
121+
~~
122+
!!! error TS2322: Type 'StrNum' is not assignable to type '[string]'.
123+
!!! error TS2322: Types of property 'pop' are incompatible.
124+
!!! error TS2322: Type '() => string | number' is not assignable to type '() => string'.
125+
var m3: [string] = z;
126+
~~
127+
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string]'.
128+
!!! error TS2322: Property 'length' is missing in type '{ 0: string; 1: number; }'.
129+
var n1: [number, string] = x;
130+
~~
131+
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
132+
!!! error TS2322: Types of property '0' are incompatible.
133+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
134+
var n2: [number, string] = y;
135+
~~
136+
!!! error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.
137+
!!! error TS2322: Types of property '0' are incompatible.
138+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
139+
var n3: [number, string] = z;
140+
~~
141+
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, string]'.
142+
!!! error TS2322: Types of property '0' are incompatible.
143+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
144+
var o1: [string, number] = x;
145+
var o2: [string, number] = y;
146+
var o3: [string, number] = y;
147+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//// [arityAndOrderCompatibility01.ts]
2+
interface StrNum extends Array<string|number> {
3+
0: string;
4+
1: number;
5+
}
6+
7+
var x: [string, number];
8+
var y: StrNum
9+
var z: {
10+
0: string;
11+
1: number;
12+
}
13+
14+
var [a, b, c] = x;
15+
var [d, e, f] = y;
16+
var [g, h, i] = z;
17+
var j1: [number, number, number] = x;
18+
var j2: [number, number, number] = y;
19+
var j3: [number, number, number] = z;
20+
var k1: [string, number, number] = x;
21+
var k2: [string, number, number] = y;
22+
var k3: [string, number, number] = z;
23+
var l1: [number] = x;
24+
var l2: [number] = y;
25+
var l3: [number] = z;
26+
var m1: [string] = x;
27+
var m2: [string] = y;
28+
var m3: [string] = z;
29+
var n1: [number, string] = x;
30+
var n2: [number, string] = y;
31+
var n3: [number, string] = z;
32+
var o1: [string, number] = x;
33+
var o2: [string, number] = y;
34+
var o3: [string, number] = y;
35+
36+
37+
//// [arityAndOrderCompatibility01.js]
38+
var x;
39+
var y;
40+
var z;
41+
var a = x[0], b = x[1], c = x[2];
42+
var d = y[0], e = y[1], f = y[2];
43+
var g = z[0], h = z[1], i = z[2];
44+
var j1 = x;
45+
var j2 = y;
46+
var j3 = z;
47+
var k1 = x;
48+
var k2 = y;
49+
var k3 = z;
50+
var l1 = x;
51+
var l2 = y;
52+
var l3 = z;
53+
var m1 = x;
54+
var m2 = y;
55+
var m3 = z;
56+
var n1 = x;
57+
var n2 = y;
58+
var n3 = z;
59+
var o1 = x;
60+
var o2 = y;
61+
var o3 = y;

tests/baselines/reference/declarationsAndAssignments.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(5,16): error TS2460: Type '[number, string]' has no property '2'.
1+
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(5,16): error TS2493: Tuple type '[number, string]' with length '2' cannot be assigned to tuple with length '3'.
22
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(56,17): error TS2322: Type 'number' is not assignable to type 'string'.
3-
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(63,13): error TS2460: Type '[number]' has no property '1'.
4-
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(63,16): error TS2460: Type '[number]' has no property '2'.
3+
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(63,13): error TS2493: Tuple type '[number]' with length '1' cannot be assigned to tuple with length '3'.
4+
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(63,16): error TS2493: Tuple type '[number]' with length '1' cannot be assigned to tuple with length '3'.
55
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(67,9): error TS2461: Type '{ [x: number]: undefined; }' is not an array type.
66
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(68,9): error TS2461: Type '{ [x: number]: number; 0: number; 1: number; }' is not an array type.
77
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,11): error TS2459: Type '{}' has no property 'a' and no string index signature.
@@ -25,7 +25,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
2525
var [x, y] = [1, "hello"];
2626
var [x, y, z] = [1, "hello"]; // Error
2727
~
28-
!!! error TS2460: Type '[number, string]' has no property '2'.
28+
!!! error TS2493: Tuple type '[number, string]' with length '2' cannot be assigned to tuple with length '3'.
2929
var [,, z] = [0, 1, 2];
3030
var x: number;
3131
var y: string;
@@ -87,9 +87,9 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
8787
var [a, b, c] = []; // Ok, [] is an array
8888
var [d, e, f] = [1]; // Error, [1] is a tuple
8989
~
90-
!!! error TS2460: Type '[number]' has no property '1'.
90+
!!! error TS2493: Tuple type '[number]' with length '1' cannot be assigned to tuple with length '3'.
9191
~
92-
!!! error TS2460: Type '[number]' has no property '2'.
92+
!!! error TS2493: Tuple type '[number]' with length '1' cannot be assigned to tuple with length '3'.
9393
}
9494

9595
function f9() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
interface StrNum extends Array<string|number> {
2+
0: string;
3+
1: number;
4+
}
5+
6+
var x: [string, number];
7+
var y: StrNum
8+
var z: {
9+
0: string;
10+
1: number;
11+
}
12+
13+
var [a, b, c] = x;
14+
var [d, e, f] = y;
15+
var [g, h, i] = z;
16+
var j1: [number, number, number] = x;
17+
var j2: [number, number, number] = y;
18+
var j3: [number, number, number] = z;
19+
var k1: [string, number, number] = x;
20+
var k2: [string, number, number] = y;
21+
var k3: [string, number, number] = z;
22+
var l1: [number] = x;
23+
var l2: [number] = y;
24+
var l3: [number] = z;
25+
var m1: [string] = x;
26+
var m2: [string] = y;
27+
var m3: [string] = z;
28+
var n1: [number, string] = x;
29+
var n2: [number, string] = y;
30+
var n3: [number, string] = z;
31+
var o1: [string, number] = x;
32+
var o2: [string, number] = y;
33+
var o3: [string, number] = y;

0 commit comments

Comments
 (0)
0