Peter Parker Peter Parker - 5 months ago 23
AngularJS Question

Typescript: TS2496: The 'arguments' object cannot be referenced in an arrow function in ES3 and ES5. Consider using a standard function expression

I followed some tutorials like http://www.html5rocks.com/en/tutorials/frameworks/angular-websockets/ to implement socket.io in my node.js server and angular.js client, everything works fine.

However, client side, Typescript display following compilation error:

TS2496: The 'arguments' object cannot be referenced in an arrow function in ES3 and ES5. Consider using a standard function expression.


Regarding following service codes:

on(eventName:string, callback) {
this.socket.on(eventName, () => {
var args = arguments; // <-- here the compilation error
this.$rootScope.$apply(() => {
callback.apply(this.socket, args);
});
});
}

emit(eventName:string, data, callback?) {
this.socket.emit(eventName, data, () => {
var args = arguments; // <-- here the compilation error
this.$rootScope.$apply(() => {
if (callback) {
callback.apply(this.socket, args);
}
});
});
}


I tried different solutions, like the one described in the first post of https://github.com/Microsoft/TypeScript/issues/1609, which fix the compilation but break the socket.io "on" functionality (the json objects aren't interpreted anymore).

Any idea how to fix or ignore this typescript compilation error?

Answer

Use function instead of ()=>

on(eventName:string, callback) {

        var self = this;
        this.socket.on(eventName, function() {
            var args = arguments;
            self.$rootScope.$apply(() => {
                callback.apply(self.socket, args);
            });
        });
    }
Comments