Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Generics 如何在typescript中使用泛型返回类引用_Generics_Typescript - Fatal编程技术网

Generics 如何在typescript中使用泛型返回类引用

Generics 如何在typescript中使用泛型返回类引用,generics,typescript,Generics,Typescript,我正在编写一个类型工厂,以根据字符串标识符输出不同的类型,例如: export class InputComponent<T> { constructor(public value: T) { } } export const getComponentClass = (identifier: string) => { switch (identifier) { case 'textarea': return Te

我正在编写一个类型工厂,以根据字符串标识符输出不同的类型,例如:

export class InputComponent<T> {
    constructor(public value: T) {
    }
}

export const getComponentClass = (identifier: string) => {
    switch (identifier) {
        case 'textarea':
            return Textarea;
        case 'input:number':
            // here to return InputComponent<number>
        case 'input:binary':
            return Radio;
        case 'input:string':
        default:
            // here I want to return something like InputComponent<string>
    }
}
有没有办法做到这一点?或者我必须使用子类的方式为每种类型指定一个类


谢谢。

当您可以创建一个具有适当类型的类的新实例时,为什么还要使用getComponentClass函数呢

const input = new InputComponent<string>("hello world");
const input=new InputComponent(“hello world”);

当您可以创建具有适当类型的类的新实例时,为什么还要使用getComponentClass函数呢

const input = new InputComponent<string>("hello world");
const input=new InputComponent(“hello world”);

在我的应用程序中,表def来自后端,在应用程序之间共享,因此我需要一些东西来确定类类型。另外,我正在运行时使用ComponentFactoryResolve在我的应用程序中创建动态组件,表def来自后端并在应用程序之间共享,因此我需要一些东西来确定类类型。另外,我正在运行时使用ComponentFactoryResolver创建动态组件