Skip to content

Commit bf14cfa

Browse files
committed
Add empty tuple type
1 parent a32d99d commit bf14cfa

20 files changed

+201
-70
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ namespace ts {
337337

338338
const emptyStringType = getLiteralType("");
339339
const zeroType = getLiteralType(0);
340+
const emptyTupleType = createTupleType([]);
340341

341342
const resolutionTargets: TypeSystemEntity[] = [];
342343
const resolutionResults: boolean[] = [];
@@ -2659,17 +2660,8 @@ namespace ts {
26592660
return createArrayTypeNode(elementType);
26602661
}
26612662
else if (type.target.objectFlags & ObjectFlags.Tuple) {
2662-
if (typeArguments.length > 0) {
2663-
const tupleConstituentNodes = mapToTypeNodes(typeArguments.slice(0, getTypeReferenceArity(type)), context);
2664-
if (tupleConstituentNodes && tupleConstituentNodes.length > 0) {
2665-
return createTupleTypeNode(tupleConstituentNodes);
2666-
}
2667-
}
2668-
if (context.encounteredError || (context.flags & NodeBuilderFlags.AllowEmptyTuple)) {
2669-
return createTupleTypeNode([]);
2670-
}
2671-
context.encounteredError = true;
2672-
return undefined;
2663+
const tupleConstituentNodes = mapToTypeNodes(typeArguments.slice(0, getTypeReferenceArity(type)), context) || [];
2664+
return createTupleTypeNode(tupleConstituentNodes);
26732665
}
26742666
else {
26752667
const outerTypeParameters = type.target.outerTypeParameters;
@@ -10056,7 +10048,7 @@ namespace ts {
1005610048
}
1005710049

1005810050
function isTupleLikeType(type: Type): boolean {
10059-
return !!getPropertyOfType(type, "0" as __String);
10051+
return !!getPropertyOfType(type, "0" as __String) || type === emptyTupleType;
1006010052
}
1006110053

1006210054
function isUnitType(type: Type): boolean {
@@ -13437,9 +13429,7 @@ namespace ts {
1343713429
}
1343813430
}
1343913431
}
13440-
if (elementTypes.length) {
13441-
return createTupleType(elementTypes);
13442-
}
13432+
return createTupleType(elementTypes);
1344313433
}
1344413434
}
1344513435
return createArrayType(elementTypes.length ?
@@ -18809,10 +18799,7 @@ namespace ts {
1880918799

1881018800
function checkTupleType(node: TupleTypeNode) {
1881118801
// Grammar checking
18812-
const hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes);
18813-
if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) {
18814-
grammarErrorOnNode(node, Diagnostics.A_tuple_type_element_list_cannot_be_empty);
18815-
}
18802+
checkGrammarForDisallowedTrailingComma(node.elementTypes);
1881618803

1881718804
forEach(node.elementTypes, checkSourceElement);
1881818805
}

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
@@ -2663,9 +2663,8 @@ namespace ts {
26632663
AllowQualifedNameInPlaceOfIdentifier = 1 << 11,
26642664
AllowAnonymousIdentifier = 1 << 13,
26652665
AllowEmptyUnionOrIntersection = 1 << 14,
2666-
AllowEmptyTuple = 1 << 15,
26672666

2668-
IgnoreErrors = AllowThisInObjectLiteral | AllowQualifedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection | AllowEmptyTuple,
2667+
IgnoreErrors = AllowThisInObjectLiteral | AllowQualifedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection,
26692668

26702669
// State
26712670
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)