typescript - 预先不知道属性名称的打字稿类
问题描述
这是我尝试过的
type SpecificType ={a:number; b:string};
type SomeInterface {
[key: string]: SpecificType
}
class SomeClass implements SomeInterface {
field1: {
a: 1,
b: ""
}
}
问题是我收到此错误:Index signature is missing in type 'SomeClass'
如果我放入 SomeClass,它就会消失[key:string]: SpecificType
,但我想避免这种情况。
此外,TS 无法识别 SomeClass 中的字段类型。
有解决方案吗?
解决方案
type SpecificType = { a: number; b: string };
abstract class SomeAbstractClass {
[key: string]: SpecificType;
}
class SomeClass extends SomeAbstractClass {
fields1 = {
a: 1,
b: "2"
}
}
推荐阅读
- rate - 对于 prometheus 和 datadog,指标的每秒速率值会有所不同
- c# - System.Net.WebUtility.UrlEncode 和 System.Web.HttpUtility.UrlEncode 方法的区别
- angular - Angular将服务注入基类但不在子类中,并在子类中使用父服务
- c# - 在 Xamarin Forms Shell 中隐藏 TabBar
- tensorflow - 使用 MobileNet 和 Faster RCNN 检测小物体
- c# - 如何在asp.net core razor pages 2.2中设置日期格式和文化
- node.js - nods js mvc 框架与 react js 和 mongodb 一起使用
- python - put() 缺少 1 个必需的位置参数:'pk'
- php - 你能检查我的代码吗?我想使用 for 循环在列表中显示数据库中的数据
- sql - 乘以其他表中最新的先前值