【发布时间】:2021-07-16 12:45:59
【问题描述】:
您好,我想为这样的对象创建一个类型:
const z = {
name: { // this one is special
buty: ['qqqq']
},
lipa: ['xxx'],
// more keys here
};
基本上就是这样的对象
type Test = {
[key: string]: string[]
}
除了一个小例外。它总是有一个值有点不同的键名。
type Special = {
name: {
[key: string]: string[]
}
}
但是当我试图合并这两种类型时
type Test =
{ [key: string]: string[] } &
{ name: { [key: string]: string[] } };
const z: Test = {
name: { // this one is special
buty: ['qqqq']
},
lipa: ['xxx'],
// more keys here
};
我收到一个错误Type '{ buty: string[]; }' is missing the following properties from type 'string[]': length, pop, push, concat, and 26 more.(2322)。
是否可以为这样的对象创建类型?
【问题讨论】:
-
@T.J. Crowder 我不认为这是同一个问题,原来的工作在特定的界面上,这个使用通用的字符串键。这应该重新打开,因为我相信没有人会使用您链接的那个来解决这个问题。
标签: typescript