access to arguments object

Topics: Language Specification
Oct 27, 2013 at 3:55 PM
Hi,

Is there a way to access arguments object? For variable args typescript generates code which copies elements to local array which is not always desired.
        var args = [];
        for (var _i = 0; _i < (arguments.length - 0); _i++) {
            args[_i] = arguments[_i + 0];
        }
Oct 28, 2013 at 5:32 PM
@alexezh,

Not sure what you mean. It's still possible to write:
function foo(...args:string[]){
    alert(arguments[0]);
}

foo("bar"); // Alerts "bar"
Although intellisense doesn't seem to think the arguments object exists (possibly a bug), it compiles fine.
Developer
Oct 28, 2013 at 6:25 PM
If you want to avoid the codegen TypeScript uses for rest params and manually deal with the arguments array you can write a function overload that takes rest params and an implementation that does not, like so:
function foo(...xs: any[]);
function foo() {
    var r = arguments[1];
    // do stuff
}
generates this:
function foo() {
    var r = arguments[1];
    // do stuff
}