【发布时间】:2022-08-20 02:17:55
【问题描述】:
我从 API 接收数据,该数据的类型取决于其他一些信息或等于无效的(API 返回无效的如果没有价值)。我创建了泛型类(模型) 和几个扩展的类模型(样品1,样品2...)。如何定义需要类型 \'something\' 的泛型类或无效的?我想在类型中包含 null,因为我使用严格的 null 检查,这会很有帮助。
我的要求:
- 通用类
- 强制类型传递给泛型以包含 null
- 在所有类中使用严格的空检查
type theType = null | {}; class Model<Base extends theType> { public value: Base = null; public sthElse: Base = null; public defaultValue: Base = null; } class IDontWantThisApproach<Base...
标签: typescript typescript-generics