1

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;
  }
7
  • What version of WebStorm? WebStorm 11 looks fine to me. Commented Dec 22, 2015 at 10:08
  • Same version here, no errors. Make sure your project is set up to use ECMAScript 6 syntax. Commented Dec 22, 2015 at 10:13
  • I found that i only get the error if my class is defined in an angular service. Don't know how to define the class properly in an angular service Commented Dec 22, 2015 at 10:15
  • The issue is with your webstorm set up. it isnt related to angular or JS. your class declaration is good and returning it as an instance from a service is fine. something with your webstorm setup does not understand your code, which gives you 0 confidence in it as a tool because your code is good. Commented Dec 22, 2015 at 10:22
  • 1
    @GuillaumeRahbari: Your edit completely changes the question (and has a syntax error you should probably fix). It's usually frowned upon to completely change a question once it has answers. Commented Dec 22, 2015 at 11:07

3 Answers 3

5

I guess WebStorm cannot determine the type of the property this._context You might want to annotate your code in order to help WebStorm (I had the same issue):

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

  /**
   * Proxy method for getting context.
   *
   * @return {ContextInterface}
   */
   get context (){
      return this._context;
   }

   /**
    * Sets the appropriate context.
    *
    * @param {ContextInterface} context
    */
   set context (context){
      this._context = context;
   }   
}
1
  • 1
    I think this should be the accepted answer. I've run into this on WebStorm with no-offending editor inspections set. I also had to use jsdoc style comments to type the get/set methods, and to make sure that those types are compatible.
    – RYS
    Commented Sep 20, 2017 at 6:06
3

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).

2
  • It seems I don't, typescript is disabled and javascript is set to ECMAScript6 Commented Dec 22, 2015 at 10:01
  • 1
    I just ran into this in PhpStorm. The setting in question is JavaScript general errors in Editor > Inspections > JavaScript > General. Commented Jul 26, 2017 at 18:34
0

It's an issue with your Webstorm setup. Have you got a linter or something else running? Is it auto-detecting language and for some reason isnt picking JS?

The code is fine.

1
  • It seems i do not have linter, I disabled all check for javascript and i still get the error. Commented Dec 22, 2015 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.