Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow empty tuple, fixes #13126 #17762

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2640,17 +2640,8 @@ namespace ts {
return createArrayTypeNode(elementType);
}
else if (type.target.objectFlags & ObjectFlags.Tuple) {
if (typeArguments.length > 0) {
const tupleConstituentNodes = mapToTypeNodes(typeArguments.slice(0, getTypeReferenceArity(type)), context);
if (tupleConstituentNodes && tupleConstituentNodes.length > 0) {
return createTupleTypeNode(tupleConstituentNodes);
}
}
if (context.encounteredError || (context.flags & NodeBuilderFlags.AllowEmptyTuple)) {
return createTupleTypeNode([]);
}
context.encounteredError = true;
return undefined;
const tupleConstituentNodes = mapToTypeNodes(typeArguments.slice(0, getTypeReferenceArity(type)), context);
return createTupleTypeNode(tupleConstituentNodes);
}
else {
const outerTypeParameters = type.target.outerTypeParameters;
Expand Down Expand Up @@ -9991,7 +9982,7 @@ namespace ts {
}

function isTupleLikeType(type: Type): boolean {
return !!getPropertyOfType(type, "0" as __String);
return !!getPropertyOfType(type, "0" as __String) || type === getTupleTypeOfArity(0);
}

function isUnitType(type: Type): boolean {
Expand Down Expand Up @@ -13376,9 +13367,7 @@ namespace ts {
}
}
}
if (elementTypes.length) {
return createTupleType(elementTypes);
}
return createTupleType(elementTypes);
}
}
return createArrayType(elementTypes.length ?
Expand Down Expand Up @@ -18736,11 +18725,7 @@ namespace ts {

function checkTupleType(node: TupleTypeNode) {
// Grammar checking
const hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes);
if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) {
grammarErrorOnNode(node, Diagnostics.A_tuple_type_element_list_cannot_be_empty);
}

checkGrammarForDisallowedTrailingComma(node.elementTypes);
forEach(node.elementTypes, checkSourceElement);
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2651,9 +2651,9 @@ namespace ts {
AllowQualifedNameInPlaceOfIdentifier = 1 << 11,
AllowAnonymousIdentifier = 1 << 13,
AllowEmptyUnionOrIntersection = 1 << 14,
AllowEmptyTuple = 1 << 15,
// AllowEmptyTuple = 1 << 15,

IgnoreErrors = AllowThisInObjectLiteral | AllowQualifedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection | AllowEmptyTuple,
IgnoreErrors = AllowThisInObjectLiteral | AllowQualifedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection,

// State
InObjectTypeLiteral = 1 << 20,
Expand Down
7 changes: 0 additions & 7 deletions tests/baselines/reference/TupleType3.errors.txt

This file was deleted.

4 changes: 4 additions & 0 deletions tests/baselines/reference/TupleType3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/parser/ecmascript5/TupleTypes/TupleType3.ts ===
var v: []
>v : Symbol(v, Decl(TupleType3.ts, 0, 3))

4 changes: 4 additions & 0 deletions tests/baselines/reference/TupleType3.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/conformance/parser/ecmascript5/TupleTypes/TupleType3.ts ===
var v: []
>v : []

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
tests/cases/compiler/anyIndexedAccessArrayNoException.ts(1,12): error TS1122: A tuple type element list cannot be empty.
tests/cases/compiler/anyIndexedAccessArrayNoException.ts(1,12): error TS2538: Type '[]' cannot be used as an index type.


==== tests/cases/compiler/anyIndexedAccessArrayNoException.ts (2 errors) ====
==== tests/cases/compiler/anyIndexedAccessArrayNoException.ts (1 errors) ====
var x: any[[]];
~~
!!! error TS1122: A tuple type element list cannot be empty.
~~
!!! error TS2538: Type '[]' cannot be used as an index type.

8 changes: 4 additions & 4 deletions tests/baselines/reference/arrayLiterals3.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type 'undefined[]' is not assignable to type '[any, any, any]'.
Property '0' is missing in type 'undefined[]'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
Property '0' is missing in type '[]'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,5): error TS2322: Type '["string", number, boolean]' is not assignable to type '[boolean, string, number]'.
Type '"string"' is not assignable to type 'boolean'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
Expand Down Expand Up @@ -32,8 +32,8 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error

var a0: [any, any, any] = []; // Error
~~
!!! error TS2322: Type 'undefined[]' is not assignable to type '[any, any, any]'.
!!! error TS2322: Property '0' is missing in type 'undefined[]'.
!!! error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
!!! error TS2322: Property '0' is missing in type '[]'.
var a1: [boolean, string, number] = ["string", 1, true]; // Error
~~
!!! error TS2322: Type '["string", number, boolean]' is not assignable to type '[boolean, string, number]'.
Expand Down

This file was deleted.

8 changes: 8 additions & 0 deletions tests/baselines/reference/emptyTuplesTypeAssertion01.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts ===
let x = <[]>[];
>x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 0, 3))

let y = x[0];
>y : Symbol(y, Decl(emptyTuplesTypeAssertion01.ts, 1, 3))
>x : Symbol(x, Decl(emptyTuplesTypeAssertion01.ts, 0, 3))

12 changes: 12 additions & 0 deletions tests/baselines/reference/emptyTuplesTypeAssertion01.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts ===
let x = <[]>[];
>x : []
><[]>[] : []
>[] : []

let y = x[0];
>y : never
>x[0] : never
>x : []
>0 : 0

This file was deleted.

8 changes: 8 additions & 0 deletions tests/baselines/reference/emptyTuplesTypeAssertion02.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts ===
let x = [] as [];
>x : Symbol(x, Decl(emptyTuplesTypeAssertion02.ts, 0, 3))

let y = x[0];
>y : Symbol(y, Decl(emptyTuplesTypeAssertion02.ts, 1, 3))
>x : Symbol(x, Decl(emptyTuplesTypeAssertion02.ts, 0, 3))

12 changes: 12 additions & 0 deletions tests/baselines/reference/emptyTuplesTypeAssertion02.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts ===
let x = [] as [];
>x : []
>[] as [] : []
>[] : []

let y = x[0];
>y : never
>x[0] : never
>x : []
>0 : 0

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(35,21): error
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(36,21): error TS2538: Type 'boolean' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(41,31): error TS2538: Type 'boolean' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(46,16): error TS2538: Type 'boolean' cannot be used as an index type.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(49,12): error TS1122: A tuple type element list cannot be empty.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(63,33): error TS2345: Argument of type '"size"' is not assignable to parameter of type '"name" | "width" | "height" | "visible"'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(64,33): error TS2345: Argument of type '"name" | "size"' is not assignable to parameter of type '"name" | "width" | "height" | "visible"'.
Type '"size"' is not assignable to type '"name" | "width" | "height" | "visible"'.
Expand All @@ -29,7 +28,7 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(76,5): error
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(77,5): error TS2322: Type 'keyof (T & U)' is not assignable to type 'keyof (T | U)'.


==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (25 errors) ====
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (24 errors) ====
class Shape {
name: string;
width: number;
Expand Down Expand Up @@ -113,8 +112,6 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(77,5): error

type T60 = {}["toString"];
type T61 = []["toString"];
~~
!!! error TS1122: A tuple type element list cannot be empty.

declare let cond: boolean;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
tests/cases/compiler/promiseEmptyTupleNoException.ts(1,38): error TS1122: A tuple type element list cannot be empty.
tests/cases/compiler/promiseEmptyTupleNoException.ts(3,3): error TS2322: Type 'any[]' is not assignable to type '[]'.
Types of property 'pop' are incompatible.
Type '() => any' is not assignable to type '() => never'.
Type 'any' is not assignable to type 'never'.


==== tests/cases/compiler/promiseEmptyTupleNoException.ts (2 errors) ====
==== tests/cases/compiler/promiseEmptyTupleNoException.ts (1 errors) ====
export async function get(): Promise<[]> {
~~
!!! error TS1122: A tuple type element list cannot be empty.
let emails = [];
return emails;
~~~~~~~~~~~~~~
Expand Down
71 changes: 54 additions & 17 deletions tests/baselines/reference/tupleTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
tests/cases/compiler/tupleTypes.ts(1,9): error TS1122: A tuple type element list cannot be empty.
tests/cases/compiler/tupleTypes.ts(14,1): error TS2322: Type 'undefined[]' is not assignable to type '[number, string]'.
Property '0' is missing in type 'undefined[]'.
tests/cases/compiler/tupleTypes.ts(15,1): error TS2322: Type '[number]' is not assignable to type '[number, string]'.
tests/cases/compiler/tupleTypes.ts(15,1): error TS2322: Type '[]' is not assignable to type '[number, string]'.
Property '0' is missing in type '[]'.
tests/cases/compiler/tupleTypes.ts(16,1): error TS2322: Type '[number]' is not assignable to type '[number, string]'.
Property '1' is missing in type '[number]'.
tests/cases/compiler/tupleTypes.ts(17,1): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
tests/cases/compiler/tupleTypes.ts(18,1): error TS2322: Type '[string, number]' is not assignable to type '[number, string]'.
Type 'string' is not assignable to type 'number'.
tests/cases/compiler/tupleTypes.ts(41,1): error TS2322: Type 'undefined[]' is not assignable to type '[number, string]'.
tests/cases/compiler/tupleTypes.ts(47,1): error TS2322: Type '[number, string]' is not assignable to type 'number[]'.
tests/cases/compiler/tupleTypes.ts(25,1): error TS2322: Type '[number]' is not assignable to type '[]'.
Types of property 'pop' are incompatible.
Type '() => number' is not assignable to type '() => never'.
Type 'number' is not assignable to type 'never'.
tests/cases/compiler/tupleTypes.ts(48,1): error TS2322: Type '[]' is not assignable to type '[number, string]'.
tests/cases/compiler/tupleTypes.ts(55,1): error TS2322: Type '[number, string]' is not assignable to type 'number[]'.
Types of property 'pop' are incompatible.
Type '() => string | number' is not assignable to type '() => number'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/compiler/tupleTypes.ts(49,1): error TS2322: Type '[number, {}]' is not assignable to type 'number[]'.
tests/cases/compiler/tupleTypes.ts(57,1): error TS2322: Type '[number, {}]' is not assignable to type 'number[]'.
Types of property 'pop' are incompatible.
Type '() => number | {}' is not assignable to type '() => number'.
Type 'number | {}' is not assignable to type 'number'.
Type '{}' is not assignable to type 'number'.
tests/cases/compiler/tupleTypes.ts(50,1): error TS2322: Type '[number, number]' is not assignable to type '[number, string]'.
tests/cases/compiler/tupleTypes.ts(59,1): error TS2322: Type '[number, number]' is not assignable to type '[number, string]'.
Type 'number' is not assignable to type 'string'.
tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is not assignable to type '[number, string]'.
tests/cases/compiler/tupleTypes.ts(60,1): error TS2322: Type '[number, {}]' is not assignable to type '[number, string]'.
Type '{}' is not assignable to type 'string'.
tests/cases/compiler/tupleTypes.ts(61,1): error TS2322: Type '[]' is not assignable to type '[number, string]'.
tests/cases/compiler/tupleTypes.ts(64,1): error TS2322: Type '[]' is not assignable to type '[number, {}]'.
Property '0' is missing in type '[]'.
tests/cases/compiler/tupleTypes.ts(65,1): error TS2322: Type '[number, string]' is not assignable to type '[]'.
Types of property 'pop' are incompatible.
Type '() => string | number' is not assignable to type '() => never'.
Type 'string | number' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.


==== tests/cases/compiler/tupleTypes.ts (9 errors) ====
var v1: []; // Error
~~
!!! error TS1122: A tuple type element list cannot be empty.
==== tests/cases/compiler/tupleTypes.ts (12 errors) ====
var v1: [] = [];
var v2: [number];
var v3: [number, string];
var v4: [number, [string, string]];
var v5: number[] = v1;

var t: [number, string];
var t0 = t[0]; // number
Expand All @@ -40,8 +50,8 @@ tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is n

t = []; // Error
~
!!! error TS2322: Type 'undefined[]' is not assignable to type '[number, string]'.
!!! error TS2322: Property '0' is missing in type 'undefined[]'.
!!! error TS2322: Type '[]' is not assignable to type '[number, string]'.
!!! error TS2322: Property '0' is missing in type '[]'.
t = [1]; // Error
~
!!! error TS2322: Type '[number]' is not assignable to type '[number, string]'.
Expand All @@ -53,6 +63,17 @@ tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is n
!!! error TS2322: Type 'string' is not assignable to type 'number'.
t = [1, "hello", 2]; // Ok

var et: [];
var et0 = et[0]; // never
var et0: never;
et = []; // Ok
et = [1]; // Error
~~
!!! error TS2322: Type '[number]' is not assignable to type '[]'.
!!! error TS2322: Types of property 'pop' are incompatible.
!!! error TS2322: Type '() => number' is not assignable to type '() => never'.
!!! error TS2322: Type 'number' is not assignable to type 'never'.

var tf: [string, (x: string) => number] = ["hello", x => x.length];

declare function ff<T, U>(a: T, b: [T, (x: T) => U]): U;
Expand All @@ -76,12 +97,13 @@ tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is n
tt = [undefined, undefined];
tt = []; // Error
~~
!!! error TS2322: Type 'undefined[]' is not assignable to type '[number, string]'.
!!! error TS2322: Type '[]' is not assignable to type '[number, string]'.

var a: number[];
var a1: [number, string];
var a2: [number, number];
var a3: [number, {}];
var a4: [];
a = a1; // Error
~
!!! error TS2322: Type '[number, string]' is not assignable to type 'number[]'.
Expand All @@ -97,6 +119,7 @@ tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is n
!!! error TS2322: Type '() => number | {}' is not assignable to type '() => number'.
!!! error TS2322: Type 'number | {}' is not assignable to type 'number'.
!!! error TS2322: Type '{}' is not assignable to type 'number'.
a = a4;
a1 = a2; // Error
~~
!!! error TS2322: Type '[number, number]' is not assignable to type '[number, string]'.
Expand All @@ -105,6 +128,20 @@ tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is n
~~
!!! error TS2322: Type '[number, {}]' is not assignable to type '[number, string]'.
!!! error TS2322: Type '{}' is not assignable to type 'string'.
a1 = a4; // Error
~~
!!! error TS2322: Type '[]' is not assignable to type '[number, string]'.
a3 = a1;
a3 = a2;
a3 = a4; // Error
~~
!!! error TS2322: Type '[]' is not assignable to type '[number, {}]'.
!!! error TS2322: Property '0' is missing in type '[]'.
a4 = a1; // Error
~~
!!! error TS2322: Type '[number, string]' is not assignable to type '[]'.
!!! error TS2322: Types of property 'pop' are incompatible.
!!! error TS2322: Type '() => string | number' is not assignable to type '() => never'.
!!! error TS2322: Type 'string | number' is not assignable to type 'never'.
!!! error TS2322: Type 'string' is not assignable to type 'never'.

Loading