Skip to content

Commit 3fb0840

Browse files
authored
Merge pull request #10466 from Microsoft/tupleTypeReferences
Unify representation of tuples and other generic types
2 parents 4a58e68 + 2013058 commit 3fb0840

18 files changed

+162
-217
lines changed

src/compiler/checker.ts

Lines changed: 97 additions & 93 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,7 @@ namespace ts {
22662266
Class = 1 << 15, // Class
22672267
Interface = 1 << 16, // Interface
22682268
Reference = 1 << 17, // Generic type reference
2269-
Tuple = 1 << 18, // Tuple
2269+
Tuple = 1 << 18, // Synthesized generic tuple type
22702270
Union = 1 << 19, // Union (T | U)
22712271
Intersection = 1 << 20, // Intersection (T & U)
22722272
Anonymous = 1 << 21, // Anonymous
@@ -2388,11 +2388,6 @@ namespace ts {
23882388
instantiations: Map<TypeReference>; // Generic instantiation cache
23892389
}
23902390

2391-
export interface TupleType extends ObjectType {
2392-
elementTypes: Type[]; // Element types
2393-
thisType?: Type; // This-type of tuple (only needed for tuples that are constraints of type parameters)
2394-
}
2395-
23962391
export interface UnionOrIntersectionType extends Type {
23972392
types: Type[]; // Constituent types
23982393
/* @internal */

tests/baselines/reference/arityAndOrderCompatibility01.errors.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(26,5): error
3939
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(27,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[string]'.
4040
Property 'length' is missing in type '{ 0: string; 1: number; }'.
4141
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(28,5): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
42-
Types of property '0' are incompatible.
43-
Type 'string' is not assignable to type 'number'.
42+
Type 'string' is not assignable to type 'number'.
4443
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.
4544
Types of property '0' are incompatible.
4645
Type 'string' is not assignable to type 'number'.
@@ -136,8 +135,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
136135
var n1: [number, string] = x;
137136
~~
138137
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
139-
!!! error TS2322: Types of property '0' are incompatible.
140-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
138+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
141139
var n2: [number, string] = y;
142140
~~
143141
!!! error TS2322: Type 'StrNum' is not assignable to type '[number, string]'.

tests/baselines/reference/arrayLiterals3.errors.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type 'undefined[]' is not assignable to type '[any, any, any]'.
22
Property '0' is missing in type 'undefined[]'.
33
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,5): error TS2322: Type '[string, number, boolean]' is not assignable to type '[boolean, string, number]'.
4-
Types of property '0' are incompatible.
5-
Type 'string' is not assignable to type 'boolean'.
4+
Type 'string' is not assignable to type 'boolean'.
65
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
76
Types of property 'pop' are incompatible.
87
Type '() => string | number | boolean' is not assignable to type '() => number'.
@@ -37,8 +36,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
3736
var a1: [boolean, string, number] = ["string", 1, true]; // Error
3837
~~
3938
!!! error TS2322: Type '[string, number, boolean]' is not assignable to type '[boolean, string, number]'.
40-
!!! error TS2322: Types of property '0' are incompatible.
41-
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
39+
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
4240

4341
// The resulting type an array literal expression is determined as follows:
4442
// - If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.17.1),

tests/baselines/reference/castingTuple.errors.txt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
tests/cases/conformance/types/tuple/castingTuple.ts(28,10): error TS2352: Type '[number, string]' cannot be converted to type '[number, number]'.
2-
Types of property '1' are incompatible.
3-
Type 'string' is not comparable to type 'number'.
2+
Type 'string' is not comparable to type 'number'.
43
tests/cases/conformance/types/tuple/castingTuple.ts(29,10): error TS2352: Type '[C, D]' cannot be converted to type '[A, I]'.
5-
Types of property '0' are incompatible.
6-
Type 'C' is not comparable to type 'A'.
7-
Property 'a' is missing in type 'C'.
4+
Type 'C' is not comparable to type 'A'.
5+
Property 'a' is missing in type 'C'.
86
tests/cases/conformance/types/tuple/castingTuple.ts(30,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'array1' must be of type '{}[]', but here has type 'number[]'.
97
tests/cases/conformance/types/tuple/castingTuple.ts(31,1): error TS2304: Cannot find name 't4'.
108

@@ -40,14 +38,12 @@ tests/cases/conformance/types/tuple/castingTuple.ts(31,1): error TS2304: Cannot
4038
var t3 = <[number, number]>numStrTuple;
4139
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4240
!!! error TS2352: Type '[number, string]' cannot be converted to type '[number, number]'.
43-
!!! error TS2352: Types of property '1' are incompatible.
44-
!!! error TS2352: Type 'string' is not comparable to type 'number'.
41+
!!! error TS2352: Type 'string' is not comparable to type 'number'.
4542
var t9 = <[A, I]>classCDTuple;
4643
~~~~~~~~~~~~~~~~~~~~
4744
!!! error TS2352: Type '[C, D]' cannot be converted to type '[A, I]'.
48-
!!! error TS2352: Types of property '0' are incompatible.
49-
!!! error TS2352: Type 'C' is not comparable to type 'A'.
50-
!!! error TS2352: Property 'a' is missing in type 'C'.
45+
!!! error TS2352: Type 'C' is not comparable to type 'A'.
46+
!!! error TS2352: Property 'a' is missing in type 'C'.
5147
var array1 = <number[]>numStrTuple;
5248
~~~~~~
5349
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'array1' must be of type '{}[]', but here has type 'number[]'.

tests/baselines/reference/contextualTypeWithTuple.errors.txt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(3,5): error TS232
55
Type 'true' is not assignable to type 'string | number'.
66
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(15,1): error TS2322: Type '[number, string, boolean]' is not assignable to type '[number, string]'.
77
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(18,1): error TS2322: Type '[{}, number]' is not assignable to type '[{ a: string; }, number]'.
8-
Types of property '0' are incompatible.
9-
Type '{}' is not assignable to type '{ a: string; }'.
10-
Property 'a' is missing in type '{}'.
8+
Type '{}' is not assignable to type '{ a: string; }'.
9+
Property 'a' is missing in type '{}'.
1110
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(19,1): error TS2322: Type '[number, string]' is not assignable to type '[number, string, boolean]'.
1211
Property '2' is missing in type '[number, string]'.
1312
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(20,5): error TS2322: Type '[string, string, number]' is not assignable to type '[string, string]'.
@@ -18,9 +17,8 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(20,5): error TS23
1817
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(24,1): error TS2322: Type '[C, string | number]' is not assignable to type '[C, string | number, D]'.
1918
Property '2' is missing in type '[C, string | number]'.
2019
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(25,1): error TS2322: Type '[number, string | number]' is not assignable to type '[number, string]'.
21-
Types of property '1' are incompatible.
22-
Type 'string | number' is not assignable to type 'string'.
23-
Type 'number' is not assignable to type 'string'.
20+
Type 'string | number' is not assignable to type 'string'.
21+
Type 'number' is not assignable to type 'string'.
2422

2523

2624
==== tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts (7 errors) ====
@@ -52,9 +50,8 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(25,1): error TS23
5250
objNumTuple = [ {}, 5];
5351
~~~~~~~~~~~
5452
!!! error TS2322: Type '[{}, number]' is not assignable to type '[{ a: string; }, number]'.
55-
!!! error TS2322: Types of property '0' are incompatible.
56-
!!! error TS2322: Type '{}' is not assignable to type '{ a: string; }'.
57-
!!! error TS2322: Property 'a' is missing in type '{}'.
53+
!!! error TS2322: Type '{}' is not assignable to type '{ a: string; }'.
54+
!!! error TS2322: Property 'a' is missing in type '{}'.
5855
numStrBoolTuple = numStrTuple;
5956
~~~~~~~~~~~~~~~
6057
!!! error TS2322: Type '[number, string]' is not assignable to type '[number, string, boolean]'.
@@ -76,6 +73,5 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(25,1): error TS23
7673
numStrTuple = unionTuple3;
7774
~~~~~~~~~~~
7875
!!! error TS2322: Type '[number, string | number]' is not assignable to type '[number, string]'.
79-
!!! error TS2322: Types of property '1' are incompatible.
80-
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
81-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
76+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
77+
!!! error TS2322: Type 'number' is not assignable to type 'string'.

tests/baselines/reference/contextuallyTypedBindingInitializerNegative.errors.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTyp
1111
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(16,23): error TS2322: Type '(arg: string) => number' is not assignable to type '(s: string) => string'.
1212
Type 'number' is not assignable to type 'string'.
1313
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(21,14): error TS2322: Type '[number, number]' is not assignable to type '[string, number]'.
14-
Types of property '0' are incompatible.
15-
Type 'number' is not assignable to type 'string'.
14+
Type 'number' is not assignable to type 'string'.
1615
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(26,14): error TS2322: Type '"baz"' is not assignable to type '"foo" | "bar"'.
1716

1817

@@ -57,8 +56,7 @@ tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTyp
5756
function g({ prop = [101, 1234] }: Tuples) {}
5857
~~~~
5958
!!! error TS2322: Type '[number, number]' is not assignable to type '[string, number]'.
60-
!!! error TS2322: Types of property '0' are incompatible.
61-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
59+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
6260

6361
interface StringUnion {
6462
prop: "foo" | "bar";

tests/baselines/reference/declarationsAndAssignments.errors.txt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,14):
1818
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,11): error TS2459: Type 'undefined[]' has no property 'a' and no string index signature.
1919
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,14): error TS2459: Type 'undefined[]' has no property 'b' and no string index signature.
2020
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(106,5): error TS2345: Argument of type '[number, [string, { y: boolean; }]]' is not assignable to parameter of type '[number, [string, { x: any; y?: boolean; }]]'.
21-
Types of property '1' are incompatible.
22-
Type '[string, { y: boolean; }]' is not assignable to type '[string, { x: any; y?: boolean; }]'.
23-
Types of property '1' are incompatible.
24-
Type '{ y: boolean; }' is not assignable to type '{ x: any; y?: boolean; }'.
25-
Property 'x' is missing in type '{ y: boolean; }'.
21+
Type '[string, { y: boolean; }]' is not assignable to type '[string, { x: any; y?: boolean; }]'.
22+
Type '{ y: boolean; }' is not assignable to type '{ x: any; y?: boolean; }'.
23+
Property 'x' is missing in type '{ y: boolean; }'.
2624
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,6): error TS2322: Type 'string' is not assignable to type 'number'.
2725
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9): error TS2322: Type 'number' is not assignable to type 'string'.
2826

@@ -174,11 +172,9 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
174172
f14([2, ["abc", { y: false }]]); // Error, no x
175173
~~~~~~~~~~~~~~~~~~~~~~~~~~
176174
!!! error TS2345: Argument of type '[number, [string, { y: boolean; }]]' is not assignable to parameter of type '[number, [string, { x: any; y?: boolean; }]]'.
177-
!!! error TS2345: Types of property '1' are incompatible.
178-
!!! error TS2345: Type '[string, { y: boolean; }]' is not assignable to type '[string, { x: any; y?: boolean; }]'.
179-
!!! error TS2345: Types of property '1' are incompatible.
180-
!!! error TS2345: Type '{ y: boolean; }' is not assignable to type '{ x: any; y?: boolean; }'.
181-
!!! error TS2345: Property 'x' is missing in type '{ y: boolean; }'.
175+
!!! error TS2345: Type '[string, { y: boolean; }]' is not assignable to type '[string, { x: any; y?: boolean; }]'.
176+
!!! error TS2345: Type '{ y: boolean; }' is not assignable to type '{ x: any; y?: boolean; }'.
177+
!!! error TS2345: Property 'x' is missing in type '{ y: boolean; }'.
182178

183179
module M {
184180
export var [a, b] = [1, 2];

tests/baselines/reference/destructuringArrayBindingPatternAndAssignment2.errors.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAss
22
tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment2.ts(3,12): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
33
tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment2.ts(4,5): error TS2461: Type 'undefined' is not an array type.
44
tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment2.ts(9,5): error TS2322: Type '[number, number, string]' is not assignable to type '[number, boolean, string]'.
5-
Types of property '1' are incompatible.
6-
Type 'number' is not assignable to type 'boolean'.
5+
Type 'number' is not assignable to type 'boolean'.
76
tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment2.ts(17,6): error TS2322: Type 'string' is not assignable to type 'Number'.
87
tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAssignment2.ts(22,5): error TS2322: Type 'number[]' is not assignable to type '[number, number]'.
98
Property '0' is missing in type 'number[]'.
@@ -30,8 +29,7 @@ tests/cases/conformance/es6/destructuring/destructuringArrayBindingPatternAndAss
3029
var [b0, b1, b2]: [number, boolean, string] = [1, 2, "string"]; // Error
3130
~~~~~~~~~~~~
3231
!!! error TS2322: Type '[number, number, string]' is not assignable to type '[number, boolean, string]'.
33-
!!! error TS2322: Types of property '1' are incompatible.
34-
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
32+
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
3533
interface J extends Array<Number> {
3634
2: number;
3735
}

tests/baselines/reference/destructuringParameterDeclaration2.errors.txt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,4): error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'.
2-
Types of property '1' are incompatible.
3-
Type 'string' is not assignable to type 'number'.
2+
Type 'string' is not assignable to type 'number'.
43
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,29): error TS1005: ',' expected.
54
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(8,4): error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'.
65
Types of property 'pop' are incompatible.
@@ -32,12 +31,9 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
3231
Types of property '2' are incompatible.
3332
Type 'boolean' is not assignable to type '[[any]]'.
3433
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(40,4): error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'.
35-
Types of property '2' are incompatible.
36-
Type '[[string]]' is not assignable to type '[[number]]'.
37-
Types of property '0' are incompatible.
38-
Type '[string]' is not assignable to type '[number]'.
39-
Types of property '0' are incompatible.
40-
Type 'string' is not assignable to type 'number'.
34+
Type '[[string]]' is not assignable to type '[[number]]'.
35+
Type '[string]' is not assignable to type '[number]'.
36+
Type 'string' is not assignable to type 'number'.
4137
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(46,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
4238
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(47,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
4339
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(55,7): error TS2420: Class 'C4' incorrectly implements interface 'F2'.
@@ -62,8 +58,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
6258
a0([1, "string", [["world"]]); // Error
6359
~~~~~~~~~~~~~~~~~~~~~~~~~
6460
!!! error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'.
65-
!!! error TS2345: Types of property '1' are incompatible.
66-
!!! error TS2345: Type 'string' is not assignable to type 'number'.
61+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
6762
~
6863
!!! error TS1005: ',' expected.
6964
a0([1, 2, [["world"]], "string"]); // Error
@@ -142,12 +137,9 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
142137
c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer
143138
~~~~~~~~~~~~~~~~~~~~
144139
!!! error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'.
145-
!!! error TS2345: Types of property '2' are incompatible.
146-
!!! error TS2345: Type '[[string]]' is not assignable to type '[[number]]'.
147-
!!! error TS2345: Types of property '0' are incompatible.
148-
!!! error TS2345: Type '[string]' is not assignable to type '[number]'.
149-
!!! error TS2345: Types of property '0' are incompatible.
150-
!!! error TS2345: Type 'string' is not assignable to type 'number'.
140+
!!! error TS2345: Type '[[string]]' is not assignable to type '[[number]]'.
141+
!!! error TS2345: Type '[string]' is not assignable to type '[number]'.
142+
!!! error TS2345: Type 'string' is not assignable to type 'number'.
151143

152144
// A parameter can be marked optional by following its name or binding pattern with a question mark (?)
153145
// or by including an initializer. Initializers (including binding property or element initializers) are

0 commit comments

Comments
 (0)