Skip to content

variadic tuple types broken since 4.1.2 (same error as in 3.x.x) #41701

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

Closed
ChillkroeteTTS opened this issue Nov 26, 2020 · 4 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ChillkroeteTTS
Copy link

ChillkroeteTTS commented Nov 26, 2020

TypeScript Version:
4.1.2 and newer

Search Terms:
rest elements, variadic tuple types

Code

type Numbers = number[];
type test = [...Numbers, string];

Expected behavior:
It should compile as it does in 4.0.5

Actual behavior:
tsc fails with the following (3.x like) error message

A rest element must be last in a tuple type.

Playground Link:
https://www.typescriptlang.org/play?ts=4.1.2#code/C4TwDgpgBAcgrgWwEYQE4GcoF4oDtEqoDaAugNwBQokUwE6w2URAdG-MmugDRQOoBLXAHNyQA

Related Issues:

@whzx5byb
Copy link

related: #41544

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 4, 2020
@RyanCavanaugh
Copy link
Member

This is a correct error since the output type is absolutely not what you think it will be. The linked PR will make it not an error and make it do what you expect

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@joshkel
Copy link

joshkel commented Dec 31, 2020

I ran into this today while experimenting with tuple types. If I understand correctly, this is exactly the case that was advertised as TypeScript 4.0's "variadic tuple types":

But with TypeScript 4.0, this restriction is relaxed.

Note that in cases when we spread in a type without a known length, the resulting type becomes unbounded as well, and all the following elements factor into the resulting rest element type.

type Strings = [string, string];
type Numbers = number[];

type Unbounded = [...Strings, ...Numbers, boolean];

That example from the TypeScript 4.0 docs is working in 4.0 (Playground Link) and not working in 4.1 (Playground Link), so it does seem like a regression in 4.1 rather than "working as intended"?

Or am I misunderstanding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants