我很難找出流量抱怨的問題。我試圖通過存儲實現類來允許實現一個API,然後再實例化它,但是,當我呼叫new this.implKlass
說「構造函數不能在對象類型上調用」時,流程投訴。試圖告訴我什麼是流程,以及我在概念上缺少流程的工作原理?如何解決這個「構造函數不能在對象類型上調用」錯誤的流程?
實施例下面的代碼,和flow try code這裏
/* @flow */
type ApiT = {
fnA(): Promise<*>;
}
// An implementation of the API
class Impl {
async fnA(): Promise<*> { return 1; }
}
class DoThings {
implKlass: ApiT;
constructor(klass) {
this.implKlass = klass;
}
callA() {
const Klass = this.implKlass;
const inst = new Klass();
return inst.fnA();
}
}
new DoThings(Impl).callA();
輸出示例:
18: const inst = new Klass();
^constructor call. Constructor cannot be called on
18: const inst = new Klass();
^object type
13: constructor(klass: ApiT) {
^property `fnA`. Property not found in
23: new DoThings(Impl).callA();
^statics of Impl
你需要決定是否'ApiT'是指一個類的實例,或者創建一個實例的構造函數。在這裏你將它用作兩者。 –
@RyanCavanaugh,謝謝,這是我錯過的知識缺口。 – Richard