Skip to content

Function called with spread operator doesn't typecheck even when the length of array and types of arguments are known at compile time. #22112

@alankila

Description

@alankila

TypeScript Version: 2.8.0-dev.20180222.

Search Terms:

I've looked for anything mentioning spread and type inference. I did not find this precise issue mentioned anywhere, which is why I'm adding it now.

Code

Save the following file as test.ts, then compile with tsc test.ts.

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
    function calledWithSpread(x: string, y: number, z: boolean) {
    }

    let x: [string, number, boolean] = ["foo", 1, false];
    calledWithSpread(...x);
    calledWithSpread(x[0], x[1], x[2]);

Expected behavior:
Since TypeScript is knows the variadic type of x, I would now expect that the spread operator to take that into account and realize that x would contain 3 values rather than some unknown number. Only the latter form currently passes type check.

Actual behavior:
test.ts(5,5): error TS2556: Expected 3 arguments, but got 0 or more.

Playground Link:

Related Issues:
This issue is probably also be covered in the variadic generics work in #5453. However, I expect that resolving just this issue would be easier than the full solution of that bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions