打字稿类型:定义具有特定条件的类似字典的对象类型
typescript type: define dictionary-like object type with a certain condition
我们如何使用打字稿定义遵循以下规则的类型:
- 有特定的键和值(例如:
srting
类型的键 1 及其值为 Date
)
- 其余部分可能(或可能没有)任何类型的键值对
图片:
{
"key1": new Date(),
"otherKey": "any value",
...
}
谢谢!
您可以使用 [key: <type>]: <type>
将自定义金额添加到 key-value 对。
const customType: {
key: Date,
[key:string]: any
} = {
key: new Date(),
"string": "another value",
"number": 10
// [...]
}
我们如何使用打字稿定义遵循以下规则的类型:
- 有特定的键和值(例如:
srting
类型的键 1 及其值为Date
) - 其余部分可能(或可能没有)任何类型的键值对
图片:
{
"key1": new Date(),
"otherKey": "any value",
...
}
谢谢!
您可以使用 [key: <type>]: <type>
将自定义金额添加到 key-value 对。
const customType: {
key: Date,
[key:string]: any
} = {
key: new Date(),
"string": "another value",
"number": 10
// [...]
}