【问题标题】:Flow - Property not found in statics of class流 - 在类的静态中找不到属性
【发布时间】:2017-08-17 23:05:37
【问题描述】:

给定一个具有静态属性的类,以及通过this.constructor 访问该属性的方法:

class Matematica {
  reset() {
    this.constante = this.constructor.PI;
  }
}

Matematica.PI = 3.14

流找不到静态属性PI

xx: this.constructor.PI;
                     ^^ property `PI`. Property not found in
xx: this.constructor.PI;
    ^^^^ statics of Matematica

xx: Matematica.PI = {
               ^^ property `PI`. Property not found in
xx: Matematica.PI = {
    ^^^^^^^^^^ statics of Matematica

为什么 Flow 对这个工作代码不满意,我可以做些什么来让它在代码或流程配置中满意?

【问题讨论】:

  • 一种解决方案是根本不让它成为静态的,而让它成为文件中的一个变量。
  • 你能在没有实现的情况下向类添加像static PI: number这样的类型声明吗?

标签: javascript ecmascript-6 flowtype


【解决方案1】:

找到解决办法:

class Matematica {
  reset() {
    this.constante = this.constructor.PI;
  }

  static get PI() {
    return 3.14
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-10
    • 2014-01-29
    • 2018-06-28
    • 2012-10-15
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多