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

Consistently error when rest element isn't last in tuple type #40254

Merged
merged 7 commits into from
Sep 8, 2020
Merged
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
16 changes: 11 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31121,6 +31121,7 @@ namespace ts {
function checkTupleType(node: TupleTypeNode) {
const elementTypes = node.elements;
let seenOptionalElement = false;
let seenRestElement = false;
const hasNamedElement = some(elementTypes, isNamedTupleMember);
for (let i = 0; i < elementTypes.length; i++) {
const e = elementTypes[i];
Expand All @@ -31130,16 +31131,17 @@ namespace ts {
}
const flags = getTupleElementFlags(e);
if (flags & ElementFlags.Variadic) {
if (!isArrayLikeType(getTypeFromTypeNode((<RestTypeNode | NamedTupleMember>e).type))) {
const type = getTypeFromTypeNode((<RestTypeNode | NamedTupleMember>e).type);
if (!isArrayLikeType(type)) {
error(e, Diagnostics.A_rest_element_type_must_be_an_array_type);
break;
}
if (isArrayType(type) || isTupleType(type) && type.target.combinedFlags & ElementFlags.Rest) {
seenRestElement = true;
}
}
else if (flags & ElementFlags.Rest) {
if (i !== elementTypes.length - 1) {
grammarErrorOnNode(e, Diagnostics.A_rest_element_must_be_last_in_a_tuple_type);
break;
}
seenRestElement = true;
}
else if (flags & ElementFlags.Optional) {
seenOptionalElement = true;
Expand All @@ -31148,6 +31150,10 @@ namespace ts {
grammarErrorOnNode(e, Diagnostics.A_required_element_cannot_follow_an_optional_element);
break;
}
if (seenRestElement && i !== elementTypes.length - 1) {
grammarErrorOnNode(e, Diagnostics.A_rest_element_must_be_last_in_a_tuple_type);
break;
}
}
forEach(node.elements, checkSourceElement);
}
Expand Down
21 changes: 20 additions & 1 deletion tests/baselines/reference/variadicTuples1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(203,5): error TS2322: Typ
Type '"2"' is not assignable to type 'number | "0" | "length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight" | "1"'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(346,14): error TS7019: Rest parameter 'x' implicitly has an 'any[]' type.
tests/cases/conformance/types/tuple/variadicTuples1.ts(354,26): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(393,19): error TS1256: A rest element must be last in a tuple type.
tests/cases/conformance/types/tuple/variadicTuples1.ts(396,20): error TS1256: A rest element must be last in a tuple type.
tests/cases/conformance/types/tuple/variadicTuples1.ts(397,12): error TS1256: A rest element must be last in a tuple type.


==== tests/cases/conformance/types/tuple/variadicTuples1.ts (20 errors) ====
==== tests/cases/conformance/types/tuple/variadicTuples1.ts (23 errors) ====
// Variadics in tuple types

type TV0<T extends unknown[]> = [string, ...T];
Expand Down Expand Up @@ -496,4 +499,20 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(354,26): error TS2322: Ty

callApi(getUser);
callApi(getOrgUser);

// Repro from #40235

type Numbers = number[];
type Unbounded = [...Numbers, boolean];
~~~~~~~~~~
!!! error TS1256: A rest element must be last in a tuple type.
const data: Unbounded = [false, false];

type U1 = [string, ...Numbers, boolean];
~~~~~~~~~~
!!! error TS1256: A rest element must be last in a tuple type.
type U2 = [...[string, ...Numbers], boolean];
~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1256: A rest element must be last in a tuple type.
type U3 = [...[string, number], boolean];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I get this right, does this mean we still have (or bring back) the constraint that a rest element must be at last in a tuple?
In 4.0 doc, I can see the below update:

The second change is that rest elements can occur anywhere in a tuple - not just at the end!
Previously, TypeScript would issue an error like the following:
But with TypeScript 4.0, this restriction is relaxed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


17 changes: 17 additions & 0 deletions tests/baselines/reference/variadicTuples1.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ function callApi<T extends unknown[] = [], U = void>(method: (...args: [...T, ob

callApi(getUser);
callApi(getOrgUser);

// Repro from #40235

type Numbers = number[];
type Unbounded = [...Numbers, boolean];
const data: Unbounded = [false, false];

type U1 = [string, ...Numbers, boolean];
type U2 = [...[string, ...Numbers], boolean];
type U3 = [...[string, number], boolean];


//// [variadicTuples1.js]
Expand Down Expand Up @@ -609,6 +619,7 @@ function callApi(method) {
}
callApi(getUser);
callApi(getOrgUser);
var data = [false, false];


//// [variadicTuples1.d.ts]
Expand Down Expand Up @@ -774,3 +785,9 @@ declare function getOrgUser(id: string, orgId: number, options?: {
z?: boolean;
}): void;
declare function callApi<T extends unknown[] = [], U = void>(method: (...args: [...T, object]) => U): (...args_0: T) => U;
declare type Numbers = number[];
declare type Unbounded = [...Numbers, boolean];
declare const data: Unbounded;
declare type U1 = [string, ...Numbers, boolean];
declare type U2 = [...[string, ...Numbers], boolean];
declare type U3 = [...[string, number], boolean];
24 changes: 24 additions & 0 deletions tests/baselines/reference/variadicTuples1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,27 @@ callApi(getOrgUser);
>callApi : Symbol(callApi, Decl(variadicTuples1.ts, 380, 100))
>getOrgUser : Symbol(getOrgUser, Decl(variadicTuples1.ts, 378, 71))

// Repro from #40235

type Numbers = number[];
>Numbers : Symbol(Numbers, Decl(variadicTuples1.ts, 387, 20))

type Unbounded = [...Numbers, boolean];
>Unbounded : Symbol(Unbounded, Decl(variadicTuples1.ts, 391, 24))
>Numbers : Symbol(Numbers, Decl(variadicTuples1.ts, 387, 20))

const data: Unbounded = [false, false];
>data : Symbol(data, Decl(variadicTuples1.ts, 393, 5))
>Unbounded : Symbol(Unbounded, Decl(variadicTuples1.ts, 391, 24))

type U1 = [string, ...Numbers, boolean];
>U1 : Symbol(U1, Decl(variadicTuples1.ts, 393, 39))
>Numbers : Symbol(Numbers, Decl(variadicTuples1.ts, 387, 20))

type U2 = [...[string, ...Numbers], boolean];
>U2 : Symbol(U2, Decl(variadicTuples1.ts, 395, 40))
>Numbers : Symbol(Numbers, Decl(variadicTuples1.ts, 387, 20))

type U3 = [...[string, number], boolean];
>U3 : Symbol(U3, Decl(variadicTuples1.ts, 396, 45))

23 changes: 23 additions & 0 deletions tests/baselines/reference/variadicTuples1.types
Original file line number Diff line number Diff line change
Expand Up @@ -1383,3 +1383,26 @@ callApi(getOrgUser);
>callApi : <T extends unknown[] = [], U = void>(method: (...args_0: T, args_1: object) => U) => (...args_0: T) => U
>getOrgUser : (id: string, orgId: number, options?: { y?: number | undefined; z?: boolean | undefined; } | undefined) => void

// Repro from #40235

type Numbers = number[];
>Numbers : Numbers

type Unbounded = [...Numbers, boolean];
>Unbounded : (number | boolean)[]

const data: Unbounded = [false, false];
>data : (number | boolean)[]
>[false, false] : false[]
>false : false
>false : false

type U1 = [string, ...Numbers, boolean];
>U1 : [string, ...(number | boolean)[]]

type U2 = [...[string, ...Numbers], boolean];
>U2 : [string, ...(number | boolean)[]]

type U3 = [...[string, number], boolean];
>U3 : [string, number, boolean]

10 changes: 10 additions & 0 deletions tests/cases/conformance/types/tuple/variadicTuples1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,13 @@ function callApi<T extends unknown[] = [], U = void>(method: (...args: [...T, ob

callApi(getUser);
callApi(getOrgUser);

// Repro from #40235

type Numbers = number[];
type Unbounded = [...Numbers, boolean];
const data: Unbounded = [false, false];

type U1 = [string, ...Numbers, boolean];
type U2 = [...[string, ...Numbers], boolean];
type U3 = [...[string, number], boolean];