【发布时间】:2021-01-18 04:17:03
【问题描述】:
我有一个看起来像这样的对象:
const o: A = {
id: 'foo',
key1: 123,
key2: 456
}
id 是string 类型的已知且必需的属性。其他键是动态的并且是数字。
如何在 typescript 中定义这个?
我已经试过了:
type A = {
id: string;
[key: string]: number
} // Doesn't work
或者这个
type A = {
id: string
} & {
[key: string]: number
} // Doesn't work either
TS 不会接受 ID 上的字符串并期望它是一个数字
有什么办法吗?
谢谢!
【问题讨论】:
标签: typescript typescript-typings