我很难找出流量抱怨的问题。我试图通过存储实现类来允许实现一个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