我有抽象类让我们说Conditions
。它会延长BoolCondtions
,TextConditions
等等......
我的界面看起来像这样:
export interface ConditionModel {type: string; class: Conditions}
但是当我使用该模型创建对象时,typecript抱怨BoolConditions
与Conditions
不兼容:
export const myConditions: ConditionModel[] = {
{type: 'bool', class: BoolConditions},
{type: 'text', class: TextConditions},
}
Typescript不支持扩展类吗?
答案 0 :(得分:2)
它应该是这样的,意味着你需要创建对象,现在你直接分配类型 - 这就是它给出错误的原因。
export const myConditions: ConditionModel[] = {
{type: 'bool', class: new BoolConditions()},
{type: 'text', class: new TextConditions{}},
}
或
export const myConditions: ConditionModel[] = {
{type: 'bool', class: {} as BoolConditions },
{type: 'text', class: {} as TextConditions},
}