Javascript 类型“number”不可分配给类型“PersonConfig”。ts(2322)
类型“number”不可分配给类型“PersonConfig”。ts2322 预期类型来自此索引签名Javascript 类型“number”不可分配给类型“PersonConfig”。ts(2322),javascript,typescript,Javascript,Typescript,类型“number”不可分配给类型“PersonConfig”。ts2322 预期类型来自此索引签名 interface Person { [Id: string]: PersonConfig } interface PersonConfig { name: string, age: number } const people: Person[] = [ { "p1": { name: "abcd", age
interface Person {
[Id: string]: PersonConfig
}
interface PersonConfig {
name: string,
age: number
}
const people: Person[] = [
{
"p1": {
name: "abcd",
age: 20
}
},
{
"p2": {
name: "efgh",
age: 78
}
}
];
现在,我想将一些其他字符串附加到人的键上,即p1和p2
预期解决方案:
const suffix:string = "sub";
const people: Person[] = [
{
"p1" + suffix: {
name: "abcd",
age: 20
}
},
{
"p2" + suffix: {
name: "efgh",
age: 78
}
}
];
这应该起作用:
const people: Person[] = [
{
["p1" + suffix]: {
name: "abcd",
age: 20
}
},
{
["p2" + suffix]: {
name: "efgh",
age: 78
}
}
];