【发布时间】:2018-11-07 14:17:54
【问题描述】:
我试图实例化一个作为参数传递给另一个类的实例,我在一个文件 ImportedClass.ts 中有这个:
export default class ImportedClass {
public constructor(something: any) {
}
public async exampleMethod() {
return "hey";
}
}
这在另一个,InstanceClass.ts:
interface GenericInterface<T> {
new(something: any): T;
}
export default class InstanceClass <T> {
private c: GenericInterface<T>;
public constructor(c: T) {
}
async work() {
const instanceTry = new this.c("hello");
instanceTry.exampleMethod();
}
}
这在另一个 ClassCaller.ts 中:
import ImportedClass from './ImportedClass';
import ImportedClass from './InstanceClass';
const simulator = new InstanceClass <ImportedClass>(ImportedClass);
然后当我这样称呼它时:
simulator.work();
它抛出这个错误:
error TS2339: Property 'exampleMethod' does not exist on type 'T'.
欢迎任何帮助,谢谢。
【问题讨论】:
标签: javascript typescript class generics