【发布时间】:2017-06-26 18:48:07
【问题描述】:
我遇到了 Javascript 和 FlowType 的问题。我要做的是返回静态类,以便我可以使用this.cls.staticProperty 之类的代码轻松引用实例方法中的类方法。举个例子:
// @flow
class A {
// This is where it fails... It cannot return Class<this>
// ERROR: Flow: `this` type. invariant position (expected `this` to occur only covariantly)
get cls(): Class<this> {
return this.constructor
}
}
现在,我知道我可以通过明确指定 A 来简单地解决这个问题,但是我在子类中遇到了问题
// @flow
class A {
// This works
get cls(): Class<A> {
return this.constructor
}
}
class B extends A {
static myProp: string
doSomething(): void {
// But, now, this doesn't work
// ERROR: Flow: property `myProp`. Property not found in statics of A
this.cls.myProp = "Hello World"
}
}
我现在可以通过在 B 上指定 cls 方法来解决这个问题,就像这样:
// @flow
class A {
// This works
get cls(): Class<A> {
return this.constructor
}
}
class B extends A {
static myProp: string
// I have to define this again for class B
get cls(): Class<B> {
return this.constructor
}
doSomething(): void {
// Works now, but now I'm violating the DRY principle
this.cls.myProp = "Hello World"
}
}
或者,我可以简单地返回Class<any>,如下所示:
// @flow
class A {
// Now, we are returning the constructor of any
get cls(): Class<any> {
return this.constructor
}
}
class B extends A {
static myProp: string
doSomething(): void {
// Works now, but now my IDE doesn't give me auto complete
// And I'm having to manually look up static properties and methods
this.cls.myProp = "Hello World"
}
}
但是,现在我的 IDE 没有给我任何类型提示或类的自动完成属性,所以我不得不从内存中输入属性和方法,这很容易出错,更不用说需要多少时间了不断查找静态方法和属性
我尝试的另一个解决方案是去掉cls getter,而是在我需要静态属性时指定实际的类名,但那里也有问题......考虑这段代码:
// @flow
class A {
static myProp: string
get staticMyProp(): string {
return A.myProp
}
}
A.myProp = "A Hello World"
class B extends A {
}
B.myProp = "B Hello World"
let bObj = new B()
console.log(bObj.staticMyProp) // Logs "A Hello World"
// But we expected "B Hello World" since we are running it from the B instance
最后一个选项....我可以每次都指定this.constructor....但我真的不想这样做...我宁愿使用this.cls,因为我就是这样' m 习惯(来自 Python 背景)
// @flow
class A {
static myProp: string
get staticMyProp(): string {
return this.constructor.myProp
}
}
A.myProp = "A Hello World"
class B extends A {
}
B.myProp = "B Hello World"
let bObj = new B()
console.log(bObj.staticMyProp) // Logs "B Hello World" as expected
我真的希望能够使用流类型使get cls(): Class<this>(上面的第一个示例)选项起作用,有什么建议吗?还是我运气不好?
【问题讨论】:
标签: javascript ecmascript-6 flowtype