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创建动态组件