Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 603
David Dal Busco Avatar asked Jun 28 '16 13:06

David Dal Busco


1 Answers

Another possibility is to use a rest parameter instead of arguments:

on(eventName:string, callback) {
  this.socket.on(eventName, (...args) => {
    this.$rootScope.$apply(() => {
      callback.apply(this.socket, args);
    });
  });
}
like image 117
David Sherret Avatar answered Sep 25 '22 10:09

David Sherret