在 ecmascript 6 中定义 setter 时出错

Gui*_*ari 1 javascript class webstorm angularjs ecmascript-6

我正在尝试在 ecmascript 6 中定义一个类。

这是代码:

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

   get context (){
      return this._context;
   }

   set context (context){
      this._context = context;
   }   
}
Run Code Online (Sandbox Code Playgroud)

但是对于 Webstorm 中的 setter,我总是遇到相同的错误:“Set accessor method has type that is not compatible with get accessor type”

我不明白为什么我会收到这个错误。我完全按照这里的解释做了:http : //es6-features.org/#GetterSetter

编辑:问题似乎只是因为我在一个角度工厂中定义了我的类。

所以我的问题是如何在角度工厂中正确定义一个类?

也许我不应该那样做。

编辑 2:这是我的角度工厂:

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;
  }
Run Code Online (Sandbox Code Playgroud)

Șer*_*iță 5

我猜 WebStorm 无法确定属性的类型this._context 您可能想要注释代码以帮助 WebStorm(我遇到了同样的问题):

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;
   }   
}
Run Code Online (Sandbox Code Playgroud)