镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

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

Activity

  1. ikatyang commented on Aug 21, 2017

    @ikatyang
    Contributor

    Seems related #5453 #17765 #17884 #17898.

  2. KiaraGrouwstra commented on Aug 21, 2017

    @KiaraGrouwstra
    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.

  3. mhegazy commented on Aug 22, 2017

    @mhegazy
    Contributor

    duplicate of #4130?

  4. jp-hoehmann commented on Aug 22, 2017

    @jp-hoehmann
    Author

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

  5. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions