扩展类 - 类型支持

时间:2018-05-28 09:30:34

标签: typescript

我有抽象类让我们说Conditions。它会延长BoolCondtionsTextConditions等等......

我的界面看起来像这样: export interface ConditionModel {type: string; class: Conditions}

但是当我使用该模型创建对象时,typecript抱怨BoolConditionsConditions不兼容:

export const myConditions: ConditionModel[] = {
  {type: 'bool', class: BoolConditions},
  {type: 'text', class: TextConditions},
}

Typescript不支持扩展类吗?

1 个答案:

答案 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},
}