Javascript typeScript初始化动态类名称调用(2345) 我如何在不使用条件语句的情况下调用类?

Javascript typeScript初始化动态类名称调用(2345) 我如何在不使用条件语句的情况下调用类?,javascript,typescript,Javascript,Typescript,问题:(参数)项:输入 “inputs”类型的参数不能分配给“ISelect&IText”类型的参数。 类型“ISelect”不可分配给类型“ISelect&IText”。 类型“ISelect”中缺少属性“placeholder”,但类型“IText”中需要属性“placeholder”。(2345) ts(12,5):“占位符”在这里声明 interface IInput { id: number title: string } interface ISelect exten

问题:(参数)项:输入 “inputs”类型的参数不能分配给“ISelect&IText”类型的参数。 类型“ISelect”不可分配给类型“ISelect&IText”。 类型“ISelect”中缺少属性“placeholder”,但类型“IText”中需要属性“placeholder”。(2345) ts(12,5):“占位符”在这里声明

interface IInput {
    id: number
    title: string
}
interface ISelect extends IInput { 
    type: "inputSelect",
    value: string
    selects: string[]
}
interface IText extends IInput{
    type: "inputText",
    placeholder: string
    value: string
}
type inputs = ISelect | IText

class InputSelect implements ISelect {
    public id: number
    public title: string
    public value: string
    public selects: string[]
    public type: ISelect['type'] = "inputSelect"

    constructor(
        inputs: ISelect
    ) { 
        this.id = inputs.id
        this.title = inputs.title
        this.value = inputs.value
        this.selects = inputs.selects
    }

    setValue(value: string) { 
        this.value = value
    }
}

class InputText implements IText {
    public id: number
    public title: string
    public value: string
    public placeholder: string
    public type: IText['type'] = "inputText"

    constructor(
        inputs: IText
    ) { 
        this.id = inputs.id
        this.title = inputs.title
        this.value = inputs.value
        this.placeholder = inputs.placeholder
    }

    setValue(value: string) { 
        this.value = value
    }
}
let b: inputs[] = [
    {
        id: 1,
        title: "some select",
        value: "",
        selects: ["Moskow", "Bejing"],
        type: "inputSelect"
    },
    {
        id: 2,
        title: "some text input",
        value: "",
        placeholder: "yourName",
        type: "inputText"
    }
]
const classes = {
    inputText: InputText,
    inputSelect: InputSelect
}

let a = b.map(item => new classes[item.type](item)) \\here error
console.log(a)

因为
b
是输入的一个联合体,您需要在
map
函数中再次缩小它,不幸的是,这是typeScript的一个限制

a=b.map(项=>
item.type=='inputSelect'?新类[item.type](项目):
item.type=='inputText'?新类[item.type](item):
未定义
);
或者,您可以执行动态类型检查,然后在
输入
联合中的任何数量的接口上只需要1个条件

const checker=(项:T,cl:unknown):cl是({new(a:T):InstanceType})=>{
返回类[项目类型]==cl;
};
设a=b.map(项=>{
const cl=类[item.type];
退货检查人(项目,cl)?新cl(项目):未定义;
}).filter((v):v不可为空=>!!v);

因为
b
是输入的一个联合体,所以需要在
map
函数中再次缩小它,不幸的是,这是typescript的一个限制

a=b.map(项=>
item.type=='inputSelect'?新类[item.type](项目):
item.type=='inputText'?新类[item.type](item):
未定义
);
或者,您可以执行动态类型检查,然后在
输入
联合中的任何数量的接口上只需要1个条件

const checker=(项:T,cl:unknown):cl是({new(a:T):InstanceType})=>{
返回类[项目类型]==cl;
};
设a=b.map(项=>{
const cl=类[item.type];
退货检查人(项目,cl)?新cl(项目):未定义;
}).filter((v):v不可为空=>!!v);

我知道这个方法,但是这个方法让我很困惑,因为如果有很多类,你必须编写一个三元运算符(或者通常是一个条件运算符,包括一个开关),代码会增长很多倍。对不起,我的英语不好。我添加了第二个选项,多一点代码,但是它支持
输入中的任何数量的接口。现在我需要理解并收紧类型脚本。我知道这个方法,但是这个方法让我困惑,因为如果有很多类,你必须编写一个三元运算符(或者通常是一个条件运算符,包括一个开关盒),代码将增长很多倍。对不起,我的英语我添加了第二个选项,多一点代码,但它支持
输入中的任何数量的接口。现在我需要理解并收紧打字脚本