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

Spread operator when passing arguments to a function #14981

Closed
noggin182 opened this issue Apr 3, 2017 · 3 comments
Closed

Spread operator when passing arguments to a function #14981

noggin182 opened this issue Apr 3, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@noggin182
Copy link

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

@cevek
Copy link

cevek commented Apr 3, 2017

Actually you should use function overload for sum6Values

private sum6Values(...args: number[]):number;
private sum6Values(arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number) {
        return arg1 + arg2 + arg3 + arg4 + arg5 + arg6;
    }

@noggin182
Copy link
Author

In this case yes, however this is just a demonstration of the problem. I came across this problem though when using a third party component. The signature of the target function I wanted to call was:
dropEvent(eventObject: FullCalendar.EventObject, delta: moment.Duration, revertFunc: Function, jsEvent: Event, ui: any, view: FullCalendar.ViewObject)
These happened to be exactly the same as what I already had in arguments so attempted to call dropEvent(...arguments);

Overloading is only a reasonable answer when you are the author of the target function, all of the arguments are of the similar type and it makes sense in that context.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2017

Duplicate of #5296 and #15375

@mhegazy mhegazy closed this as completed Apr 27, 2017
@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 27, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 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

3 participants