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

Fails to calculate arguments length, when combining tuple spread with extra arguments #29248

Closed
hpohlmeyer opened this issue Jan 3, 2019 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@hpohlmeyer
Copy link

hpohlmeyer commented Jan 3, 2019

TypeScript Version: 3.3.0-dev.20190103

Search Terms: tuple, array, spread, arguments, length

Code

const three: [number, number, number] = [1, 2, 3];
const two: [number, number] = [1, 2];
const one: [number] = [1]; 

const foo = (a: number, b: number, c: number) => undefined;

// EXPECTED: correctly realizes there are three args
foo(...three);

// EXPECTED: correctly realizes there are two args
// error: Expected 3 arguments, but got 2.
foo(...two);

// EXPECTED: correctly realizes there is one arg
// error: Expected 3 arguments, but got 1.
foo(...one);

// UNEXPECTED: does not realize there are three args
// error: Expected 3 arguments, but got 1 or more.
foo(...two, 2);

// UNEXPECTED: does not realize there are three args
// error: Expected 3 arguments, but got 1 or more.
foo(...one, 2, 3);

Expected behavior:
Correctly calculate the count of the arguments passed into foo. Add the additional arguments to the length of the tuple. Basically the last two examples should not throw an error.

Actual behavior:
When adding additional arguments after the spreaded tuple, TS fails to calculate the total amount of arguments end expects the amount of the additional arguments "+ more".

Playground Link: Link

Related Issues:

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jan 3, 2019
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.4.0 milestone Jan 3, 2019
@ahejlsberg
Copy link
Member

I'm changing this to a suggestion as the current behavior is actually intended. From the description in #24897:

When a function call includes a spread expression of a tuple type as the last argument, the spread expression corresponds to a sequence of discrete arguments of the tuple element types.

A spread expression in any position but the last is always processed as a spread of an array type, i.e. we consider it to contribute an unknown number of arguments.

A key issue with this suggestion is whether we're willing to trust that a fixed-length tuple type always has exactly the declared fixed length. We do the best we can to check it, but it is obviously easy to circumvent.

@ahejlsberg ahejlsberg added Suggestion An idea for TypeScript and removed Bug A bug in TypeScript labels Jan 6, 2019
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 3.4.0 milestone Jan 15, 2019
@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Jan 15, 2019
@hpohlmeyer
Copy link
Author

I was just going through my open bug reports and looked at my playground link.
The behavior seems to have changed with TS 4.0 and variadic tuple types.

I guess it can be closed than.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants