Search code examples
javascriptangularjsclassecmascript-6webstorm

Error when defining a setter in ecmascript 6


I'm trying to define a class in ecmascript 6.

Here is the code :

class Machine {
   constructor (){
      this._context = null;
   }

   get context (){
      return this._context;
   }

   set context (context){
      this._context = context;
   }   
}

But I always get the same error for the setter in Webstorm : "Set accessor method has type that is not compatible with get accessor type"

I don't understand why I get this error. I did exactly as explained here : http://es6-features.org/#GetterSetter

EDIT : It seems that the problem is only present because I defined my class in an angular factory.

So my question is how can I define a class properly in an angular factory ?

Maybe I'm not supposed to do it like that.

EDIT 2 : Here is my angular factory :

angular.module('frontEndApp')
  .factory('Machine', function () {

     class Machine {
        constructor (){
           this._context = null;
        }

        get context (){
           return this._context;
        }

        set context (context){
           this._context = context;
        }   
     }

     return Machine;
  }

Solution

  • Your ES6 (ES2015) code is correct. Sounds like a bug in WebStorm around the new syntax (although the word "type" is surprising, as JavaScript is loosely typed; you might want to check that you don't have it set to TypeScript or similar).