Skip to content

Commit 90f87ef

Browse files
authored
Merge pull request #17765 from tycho01/6229-known-length-tuples
add `strictTuples` flag giving tuples known length
2 parents b7d36b2 + c1c7926 commit 90f87ef

File tree

53 files changed

+847
-298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+847
-298
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7310,6 +7310,9 @@ namespace ts {
73107310
property.type = typeParameter;
73117311
properties.push(property);
73127312
}
7313+
const lengthSymbol = createSymbol(SymbolFlags.Property, "length" as __String);
7314+
lengthSymbol.type = getLiteralType(arity);
7315+
properties.push(lengthSymbol);
73137316
const type = <GenericType & InterfaceTypeWithDeclaredMembers>createObjectType(ObjectFlags.Tuple | ObjectFlags.Reference);
73147317
type.typeParameters = typeParameters;
73157318
type.outerTypeParameters = undefined;

tests/baselines/reference/arityAndOrderCompatibility01.errors.txt

+46-52
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,57 @@
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(15,12): error TS2460: Type '{ 0: string; 1: number; }' has no property '2'.
5-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(16,5): error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
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'.
3+
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'.
5+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
66
Property '2' is missing in type '[string, number]'.
7-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,5): error TS2322: Type 'StrNum' is not assignable to type '[number, number, number]'.
7+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2322: Type 'StrNum' is not assignable to type '[number, number, number]'.
88
Property '2' is missing in type 'StrNum'.
9-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, number, number]'.
10-
Property '2' is missing in type '{ 0: string; 1: number; }'.
11-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2322: Type '[string, number]' is not assignable to type '[string, number, number]'.
9+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, number, number]'.
10+
Property '2' is missing in type '{ 0: string; 1: number; length: 2; }'.
11+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(21,5): error TS2322: Type '[string, number]' is not assignable to type '[string, number, number]'.
1212
Property '2' is missing in type '[string, number]'.
13-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2322: Type 'StrNum' is not assignable to type '[string, number, number]'.
13+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(22,5): error TS2322: Type 'StrNum' is not assignable to type '[string, number, number]'.
1414
Property '2' is missing in type 'StrNum'.
15-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(21,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string, number, number]'.
16-
Property '2' is missing in type '{ 0: string; 1: number; }'.
17-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(22,5): error TS2322: Type '[string, number]' is not assignable to type '[number]'.
15+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(23,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string, number, number]'.
16+
Property '2' is missing in type '{ 0: string; 1: number; length: 2; }'.
17+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(24,5): error TS2322: Type '[string, number]' is not assignable to type '[number]'.
1818
Types of property '0' are incompatible.
1919
Type 'string' is not assignable to type 'number'.
20-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(23,5): error TS2322: Type 'StrNum' is not assignable to type '[number]'.
20+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(25,5): error TS2322: Type 'StrNum' is not assignable to type '[number]'.
2121
Types of property '0' are incompatible.
2222
Type 'string' is not assignable to type 'number'.
23-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(24,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number]'.
24-
Property 'length' is missing in type '{ 0: string; 1: number; }'.
25-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(25,5): error TS2322: Type '[string, number]' is not assignable to type '[string]'.
26-
Types of property 'pop' are incompatible.
27-
Type '() => string | number' is not assignable to type '() => string'.
28-
Type 'string | number' is not assignable to type 'string'.
29-
Type 'number' is not assignable to type 'string'.
30-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(26,5): error TS2322: Type 'StrNum' is not assignable to type '[string]'.
31-
Types of property 'pop' are incompatible.
32-
Type '() => string | number' is not assignable to type '() => string'.
33-
Type 'string | number' is not assignable to type 'string'.
34-
Type 'number' is not assignable to type 'string'.
35-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(27,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string]'.
36-
Property 'length' is missing in type '{ 0: string; 1: number; }'.
37-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(28,5): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
23+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(26,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
24+
Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
25+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(27,5): error TS2322: Type '[string, number]' is not assignable to type '[string]'.
26+
Types of property 'length' are incompatible.
27+
Type '2' is not assignable to type '1'.
28+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(28,5): error TS2322: Type 'StrNum' is not assignable to type '[string]'.
29+
Types of property 'length' are incompatible.
30+
Type '2' is not assignable to type '1'.
31+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
32+
Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
33+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
3834
Type 'string' is not assignable to type 'number'.
39-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.
35+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(31,5): error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.
4036
Types of property '0' are incompatible.
4137
Type 'string' is not assignable to type 'number'.
42-
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, string]'.
43-
Property 'length' is missing in type '{ 0: string; 1: number; }'.
38+
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, string]'.
39+
Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
4440

4541

4642
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (19 errors) ====
4743
interface StrNum extends Array<string|number> {
4844
0: string;
4945
1: number;
46+
length: 2;
5047
}
5148

5249
var x: [string, number];
5350
var y: StrNum
5451
var z: {
5552
0: string;
5653
1: number;
54+
length: 2;
5755
}
5856

5957
var [a, b, c] = x;
@@ -64,9 +62,9 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
6462
!!! error TS2460: Type 'StrNum' has no property '2'.
6563
var [g, h, i] = z;
6664
~~~~~~~~~
67-
!!! error TS2461: Type '{ 0: string; 1: number; }' is not an array type.
65+
!!! error TS2461: Type '{ 0: string; 1: number; length: 2; }' is not an array type.
6866
~
69-
!!! error TS2460: Type '{ 0: string; 1: number; }' has no property '2'.
67+
!!! error TS2460: Type '{ 0: string; 1: number; length: 2; }' has no property '2'.
7068
var j1: [number, number, number] = x;
7169
~~
7270
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
@@ -77,8 +75,8 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
7775
!!! error TS2322: Property '2' is missing in type 'StrNum'.
7876
var j3: [number, number, number] = z;
7977
~~
80-
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, number, number]'.
81-
!!! error TS2322: Property '2' is missing in type '{ 0: string; 1: number; }'.
78+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, number, number]'.
79+
!!! error TS2322: Property '2' is missing in type '{ 0: string; 1: number; length: 2; }'.
8280
var k1: [string, number, number] = x;
8381
~~
8482
!!! error TS2322: Type '[string, number]' is not assignable to type '[string, number, number]'.
@@ -89,8 +87,8 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
8987
!!! error TS2322: Property '2' is missing in type 'StrNum'.
9088
var k3: [string, number, number] = z;
9189
~~
92-
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string, number, number]'.
93-
!!! error TS2322: Property '2' is missing in type '{ 0: string; 1: number; }'.
90+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string, number, number]'.
91+
!!! error TS2322: Property '2' is missing in type '{ 0: string; 1: number; length: 2; }'.
9492
var l1: [number] = x;
9593
~~
9694
!!! error TS2322: Type '[string, number]' is not assignable to type '[number]'.
@@ -103,26 +101,22 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
103101
!!! error TS2322: Type 'string' is not assignable to type 'number'.
104102
var l3: [number] = z;
105103
~~
106-
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number]'.
107-
!!! error TS2322: Property 'length' is missing in type '{ 0: string; 1: number; }'.
104+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number]'.
105+
!!! error TS2322: Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
108106
var m1: [string] = x;
109107
~~
110108
!!! error TS2322: Type '[string, number]' is not assignable to type '[string]'.
111-
!!! error TS2322: Types of property 'pop' are incompatible.
112-
!!! error TS2322: Type '() => string | number' is not assignable to type '() => string'.
113-
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
114-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
109+
!!! error TS2322: Types of property 'length' are incompatible.
110+
!!! error TS2322: Type '2' is not assignable to type '1'.
115111
var m2: [string] = y;
116112
~~
117113
!!! error TS2322: Type 'StrNum' is not assignable to type '[string]'.
118-
!!! error TS2322: Types of property 'pop' are incompatible.
119-
!!! error TS2322: Type '() => string | number' is not assignable to type '() => string'.
120-
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
121-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
114+
!!! error TS2322: Types of property 'length' are incompatible.
115+
!!! error TS2322: Type '2' is not assignable to type '1'.
122116
var m3: [string] = z;
123117
~~
124-
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string]'.
125-
!!! error TS2322: Property 'length' is missing in type '{ 0: string; 1: number; }'.
118+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[string]'.
119+
!!! error TS2322: Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
126120
var n1: [number, string] = x;
127121
~~
128122
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
@@ -134,8 +128,8 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
134128
!!! error TS2322: Type 'string' is not assignable to type 'number'.
135129
var n3: [number, string] = z;
136130
~~
137-
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, string]'.
138-
!!! error TS2322: Property 'length' is missing in type '{ 0: string; 1: number; }'.
131+
!!! error TS2322: Type '{ 0: string; 1: number; length: 2; }' is not assignable to type '[number, string]'.
132+
!!! error TS2322: Property 'push' is missing in type '{ 0: string; 1: number; length: 2; }'.
139133
var o1: [string, number] = x;
140134
var o2: [string, number] = y;
141135
var o3: [string, number] = y;

tests/baselines/reference/arityAndOrderCompatibility01.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
interface StrNum extends Array<string|number> {
33
0: string;
44
1: number;
5+
length: 2;
56
}
67

78
var x: [string, number];
89
var y: StrNum
910
var z: {
1011
0: string;
1112
1: number;
13+
length: 2;
1214
}
1315

1416
var [a, b, c] = x;

0 commit comments

Comments
 (0)