首页 > 解决方案 > 扩展类 - 类型支持

问题描述

我有抽象类让我们说Conditions。它比扩展BoolCondtionsTextConditions等等......

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

但是,当我使用该模型创建对象时,打字稿抱怨这与以下内容不BoolConditions兼容Conditions

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

Typescript 不支持扩展类?

标签: typescript

解决方案


它应该是这样的,意味着您需要创建对象,现在您正在直接分配类型 - 这就是它给出错误的原因。

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

推荐阅读