Skip to content

Spread operator when passing arguments to a function #14981

@noggin182

Description

@noggin182

TypeScript Version: 2.2.1

Code

class Test {
    private sum6Values(arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number) {
        return arg1 + arg2 + arg3 + arg4 + arg5 + arg6;
    }

    private function1(arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number) {
        return this.sum6Values(...arguments);
    }

    private function2() {
        const array = [1, 2, 3, 4, 5, 6];
        return this.sum6Values(...array);
    }

    private function3() {
        const array = [1, 2, 3, 4, 5, 6];
        return this.sum6Values.apply(this, array);
    }
}

Expected behavior:
Function 1, 2 and 3 should all transpile. The equivalent code in ES6 JavaScript will run fine.

Actual behavior:
There are errors in functions 1 and 2 complaining that the supplied parameters do not match the signature of the call target. Function 3 is similar in behaviour and transpiles without error.

In this case, TypeScript knows the contents of the array being spread so should be able to know if the values within the array match the signature of the call target. And in cases where the contents are unknown, it should either transpile (with a possible warning), or possible a clearer error message given

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