Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将类作为泛型参数Typescript传递_Javascript_Typescript_Class_Generics - Fatal编程技术网

Javascript 将类作为泛型参数Typescript传递

Javascript 将类作为泛型参数Typescript传递,javascript,typescript,class,generics,Javascript,Typescript,Class,Generics,我正在尝试实例一个作为参数传递给另一个的类,我在一个文件ImportedClass.ts中有这个: export default class ImportedClass { public constructor(something: any) { } public async exampleMethod() { return "hey"; } } 在另一个InstanceClass.ts中: interface GenericInterface<T> {

我正在尝试实例一个作为参数传递给另一个的类,我在一个文件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();
  }
}
它抛出以下错误:

error TS2339: Property 'exampleMethod' does not exist on type 'T'.

欢迎提供任何帮助,谢谢。

如果
T
必须有一个名为
exampleMethod
的方法,您必须将其包含在
Simulator
上的
T
约束中,才能在
Simulator
中使用:

export class ImportedClass {
    public constructor(something: any) {
    }
    public async exampleMethod() {
        return "hey";
    }
}

interface GenericInterface<T> {
    new(something: any): T;
}

export class Simulator<T extends { exampleMethod(): Promise<string> }> {
    public constructor(private c: GenericInterface<T>) {
    }
    async work() {
        const instanceTry = new this.c("hello");
        await instanceTry.exampleMethod();
    }
}
const simulator = new Simulator(ImportedClass);
simulator.work()
导出类导入类{
公共构造函数(某物:任何){
}
公共异步exampleMethod(){
返回“嘿”;
}
}
接口通用接口{
新的(某物:任何):T;
}
出口类模拟器{
公共构造函数(专用c:通用接口){
}
异步工作(){
const instanceTry=newthis.c(“你好”);
wait instanceTry.exampleMethod();
}
}
常量模拟器=新模拟器(导入类);
模拟机工作()


要使上面的代码段正常工作,还有其他一些小问题需要解决,但这就是mai问题。

感谢您的回答,我对其进行了扩展,但仍然出现以下错误:TS2345:类型为“typeof ImportedClass”的参数不能分配给类型为“{exampleMethod():any;}”的参数。类型“typeof ImportedClass”中缺少属性“exampleMethod”。再次感谢。@user3314345您对
模拟器的构造函数的参数是
T
如果您希望它是构造函数(
GenericInterface
),请检查我的示例代码和游乐场链接。
error TS2339: Property 'exampleMethod' does not exist on type 'T'.
export class ImportedClass {
    public constructor(something: any) {
    }
    public async exampleMethod() {
        return "hey";
    }
}

interface GenericInterface<T> {
    new(something: any): T;
}

export class Simulator<T extends { exampleMethod(): Promise<string> }> {
    public constructor(private c: GenericInterface<T>) {
    }
    async work() {
        const instanceTry = new this.c("hello");
        await instanceTry.exampleMethod();
    }
}
const simulator = new Simulator(ImportedClass);
simulator.work()