我在弄清楚流程所抱怨的问题时遇到了麻烦。我试图通过存储实现类来允许改变API的实现,然后稍后对其进行实例化,但是,当我打电话new this.implKlass
说“不能在对象类型上调用构造函数”时,流程会抱怨。流程试图告诉我什么?从概念上讲,流程是如何缺失的?
下面的示例代码,并在此处流尝试代码
/* @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
稍作修改,即可使用。
class DoThings {
implKlass: Class<ApiT>;
constructor(klass) {
this.implKlass = klass;
}
callA() {
const Klass = this.implKlass;
const inst = new Klass();
return inst.fnA();
}
}
错误是您正在编写ApiT
而不是Class<ApiT>
。ApiT
将是一个类的实例,Class<ApiT>
而是类本身。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句