[英]How to define a type of typescript with a variable number of keys?
我有以下情况,我不知道确切如何用打字稿表达:
{
"things": {
"a": 11,
"b": 22,
.
.
.
}
}
然后,我想定义如下内容:
type Response<T> = { [_: string]: T }
因此,上面的类型表示具有键和类型T的特定值的任何响应,但是我将有很多键,如json中所述,“ a”,“ b” ...,但我找不到它的类型。
这应该工作:
interface Response<T> {
things: {
[key: string]: T;
};
}
如果您已将某些键固定a
,则可以执行以下操作:
interface Response<T, R> {
things: {
// fixed keys
a: R;
// some more fixed keys.
[key: string]: T;
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.