Skip to content

Commit 1476eb4

Browse files
lefb766
authored andcommitted
Add empty tuple type
1 parent d4c11bf commit 1476eb4

20 files changed

+201
-69
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ namespace ts {
332332

333333
const emptyStringType = getLiteralType("");
334334
const zeroType = getLiteralType(0);
335+
const emptyTupleType = createTupleType([]);
335336

336337
const resolutionTargets: TypeSystemEntity[] = [];
337338
const resolutionResults: boolean[] = [];
@@ -2621,16 +2622,8 @@ namespace ts {
26212622
return createArrayTypeNode(elementType);
26222623
}
26232624
else if (type.target.objectFlags & ObjectFlags.Tuple) {
2624-
if (typeArguments.length > 0) {
2625-
const tupleConstituentNodes = mapToTypeNodes(typeArguments.slice(0, getTypeReferenceArity(type)), context);
2626-
if (tupleConstituentNodes && tupleConstituentNodes.length > 0) {
2627-
return createTupleTypeNode(tupleConstituentNodes);
2628-
}
2629-
}
2630-
if (!context.encounteredError && !(context.flags & NodeBuilderFlags.AllowEmptyTuple)) {
2631-
context.encounteredError = true;
2632-
}
2633-
return undefined;
2625+
const tupleConstituentNodes = mapToTypeNodes(typeArguments.slice(0, getTypeReferenceArity(type)), context) || [];
2626+
return createTupleTypeNode(tupleConstituentNodes);
26342627
}
26352628
else {
26362629
const outerTypeParameters = type.target.outerTypeParameters;
@@ -9898,7 +9891,7 @@ namespace ts {
98989891
}
98999892

99009893
function isTupleLikeType(type: Type): boolean {
9901-
return !!getPropertyOfType(type, "0" as __String);
9894+
return !!getPropertyOfType(type, "0" as __String) || type === emptyTupleType;
99029895
}
99039896

99049897
function isUnitType(type: Type): boolean {
@@ -13204,9 +13197,7 @@ namespace ts {
1320413197
}
1320513198
}
1320613199
}
13207-
if (elementTypes.length) {
13208-
return createTupleType(elementTypes);
13209-
}
13200+
return createTupleType(elementTypes);
1321013201
}
1321113202
}
1321213203
return createArrayType(elementTypes.length ?
@@ -18493,10 +18484,7 @@ namespace ts {
1849318484

1849418485
function checkTupleType(node: TupleTypeNode) {
1849518486
// Grammar checking
18496-
const hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes);
18497-
if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) {
18498-
grammarErrorOnNode(node, Diagnostics.A_tuple_type_element_list_cannot_be_empty);
18499-
}
18487+
checkGrammarForDisallowedTrailingComma(node.elementTypes);
1850018488

1850118489
forEach(node.elementTypes, checkSourceElement);
1850218490
}

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,6 @@
355355
"category": "Error",
356356
"code": 1121
357357
},
358-
"A tuple type element list cannot be empty.": {
359-
"category": "Error",
360-
"code": 1122
361-
},
362358
"Variable declaration list cannot be empty.": {
363359
"category": "Error",
364360
"code": 1123

src/compiler/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,9 +2692,8 @@ namespace ts {
26922692
AllowQualifedNameInPlaceOfIdentifier = 1 << 11,
26932693
AllowAnonymousIdentifier = 1 << 13,
26942694
AllowEmptyUnionOrIntersection = 1 << 14,
2695-
AllowEmptyTuple = 1 << 15,
26962695

2697-
IgnoreErrors = AllowThisInObjectLiteral | AllowQualifedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection | AllowEmptyTuple,
2696+
IgnoreErrors = AllowThisInObjectLiteral | AllowQualifedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection,
26982697

26992698
// State
27002699
InObjectTypeLiteral = 1 << 20,

tests/baselines/reference/TupleType3.errors.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/parser/ecmascript5/TupleTypes/TupleType3.ts ===
2+
var v: []
3+
>v : Symbol(v, Decl(TupleType3.ts, 0, 3))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/parser/ecmascript5/TupleTypes/TupleType3.ts ===
2+
var v: []
3+
>v : []
4+

tests/baselines/reference/arrayLiterals3.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type 'undefined[]' is not assignable to type '[any, any, any]'.
2-
Property '0' is missing in type 'undefined[]'.
1+
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
2+
Property '0' is missing in type '[]'.
33
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,5): error TS2322: Type '["string", number, boolean]' is not assignable to type '[boolean, string, number]'.
44
Type '"string"' is not assignable to type 'boolean'.
55
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
@@ -32,8 +32,8 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
3232

3333
var a0: [any, any, any] = []; // Error
3434
~~
35-
!!! error TS2322: Type 'undefined[]' is not assignable to type '[any, any, any]'.
36-
!!! error TS2322: Property '0' is missing in type 'undefined[]'.
35+
!!! error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
36+
!!! error TS2322: Property '0' is missing in type '[]'.
3737
var a1: [boolean, string, number] = ["string", 1, true]; // Error
3838
~~
3939
!!! error TS2322: Type '["string", number, boolean]' is not assignable to type '[boolean, string, number]'.

tests/baselines/reference/emptyTuplesTypeAssertion01.errors.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts ===
2+
let x = <[]>[];
3+
>x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 0, 3))
4+
5+
let y = x[0];
6+
>y : Symbol(y, Decl(emptyTuplesTypeAssertion01.ts, 1, 3))
7+
>x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 0, 3))
8+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts ===
2+
let x = <[]>[];
3+
>x : []
4+
><[]>[] : []
5+
>[] : []
6+
7+
let y = x[0];
8+
>y : never
9+
>x[0] : never
10+
>x : []
11+
>0 : 0
12+

0 commit comments

Comments
 (0)