This project is read-only.

Can I use `arguments` without copying it to another array?

Topics: General
Dec 22, 2013 at 2:04 PM
Playground Example
function Foo(...args: any[]) : void {}

Foo(1, "", 0.5);
Why does it generate this?
    var args = [];
    for (var _i = 0; _i < (arguments.length - 0); _i++) {
        args[_i] = arguments[_i + 0];
I tried naming the parameter as arguments but then I get a compiler error. It is redundant in this case...
Dec 22, 2013 at 3:50 PM
You can use the 'arguments' variable directly without using a rest parameter. Here I use the rest parameter in an overload, so that I can still call the function with any number of arguments, but in the implementation I just use arguments directly:
function Foo(...args:any[]): any;
function Foo(): any {
    var x = arguments[0];
    return x+1;

alert(Foo(3, 4, 5));
Marked as answer by brunolm7 on 12/22/2013 at 8:06 AM