类型'number'不能分配给类型'PersonConfig'.ts(2322) 预期的类型来自此索引签名。
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
}
}
];
答案 0 :(得分:1)
这应该有效:
const people: Person[] = [
{
["p1" + suffix]: {
name: "abcd",
age: 20
}
},
{
["p2" + suffix]: {
name: "efgh",
age: 78
}
}
];