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

Proposal: Support spread operator for tuples in function calls #17934

Closed
jp-hoehmann opened this issue Aug 20, 2017 · 4 comments
Closed

Proposal: Support spread operator for tuples in function calls #17934

jp-hoehmann opened this issue Aug 20, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@jp-hoehmann
Copy link

With current versions of TypeScript, this is an error:

((_) => {})(...[null]); // ERROR: Expected 1 arguments, but got a minimum of 0.

In order to make this code work, the developer needs to tell the type checker to shut up, which could look like this:

(((_) => {}) as any)(...[null]); // Working fine.

This is unfortunate, as passing arguments for functions around in tuples is a common pattern in functional programming and there is currently no way to do it safely in TypeScript.

This was mentioned in #5296, which lead to the spread operator being supported for arrays. Supporting tuples in addition to arrays is not a straightforward change, since tuples can currently have additional values, which are not type checked (there is proposal #6229, which aims to fix this):

function f(s: string, a: number, b: number, c: number,  ...rest: number[]) {
    // ...
}
// legal
let quad: [string, number, number, number, number] = ["foo", 1,2,3,4];
f(...quad); // and triple

quad = ["foo", 1, 2, 3, 4, "boo!"]; // it is legal currently
f(...quad); // now it blows up in run time because rest has a string

Here is a longer and more detailed example taken from #5296 showing the desired behavior in detail:

function f(s: string, a: number, b: number, c: number,  ...rest: number[]) {
    // ...
}
function g(s: string, a: number, b: number) {
}
// legal
let quad: [string, number, number, number, number] = ["foo", 1,2,3,4];
let triple: [string, number, number, number] = ["foo", 1,2,3];
let double: [string, number, number] = ["foo", 1,2];
f(...quad); // and triple
f(...double, 3);
f("foo", ...[1,2,3]);
f(...quad, 1, ...[1,2,3]); // and triple
g(...double);
// illegal
f(...double); // too short -- double doesn't match parameter 'c'
g(...triple); // too many parameters
g(...quad); // too many parameters
@ikatyang
Copy link
Contributor

Seems related #5453 #17765 #17884 #17898.

@KiaraGrouwstra
Copy link
Contributor

The strict tuples should help, but wouldn't be sufficient; the request seems legitimate. afaik the code did have a check so as to match spreads in rest param positions, but yeah this may well be missing.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 22, 2017

duplicate of #4130?

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 22, 2017
@jp-hoehmann
Copy link
Author

@mhegazy whoops... didn't notice that one was still open. I'll close this.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants