typescript - 扩展类 - 类型支持
问题描述
我有抽象类让我们说Conditions
。它比扩展BoolCondtions
,TextConditions
等等......
我的界面看起来像这样:
export interface ConditionModel {type: string; class: Conditions}
但是,当我使用该模型创建对象时,打字稿抱怨这与以下内容不BoolConditions
兼容Conditions
:
export const myConditions: ConditionModel[] = {
{type: 'bool', class: BoolConditions},
{type: 'text', class: TextConditions},
}
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},
}
推荐阅读
- java - 可序列化对象中的 ArrayList 未携带 writeUnshared/readUnshared(通过网络)
- ios - 当 UIView 的 `bounds` 原点以正数增加时,为什么子视图会向负方向移动?
- python-3.x - 传递给 numpy.ndarray 的不受支持的格式字符串
- java - RecyclerView 中的 SmoothScroller
- android - Adroid - 我只在省电 GPS 模式下获得位置(以及为什么我得到它两次?)
- google-apps-script - Google Script 的前端技术
- html - 指针光标不显示
- sql - 存储过程停止工作并重新编译修复它
- java - 将光栅化器的像素链接到我的 JavaFX 应用程序的画布
- excel - VBScript(不是 VBA)将 Excel 中的复选框添加到特定单元格