我无法在打字稿中的对象中添加元素。
const bodyData = [
{
propName: "name",
value: "secondName",
},
{
propName: "surname",
value: "nothing",
},
];
const updatedData = {};
bodyData.forEach(({ propName, value }) => (updatedData[propName] = value));
console.log(updatedData);
<块引用>
元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型“{}”。 在类型“{}”上找不到参数类型为“string”的索引签名。
14 bodyData.forEach(({ propName, value }) => (updatedData[propName] = value)); ~~~~~~~~~~~~~~~~~~~~~~~~
在 createTSError (/home/mycomputer/MyTrailProjects/typescript-rest-shop/node_modules/ts-node/src/index.ts:513:12) at reportTSError (/home/mycomputer/MyTrailProjects/typescript-rest-shop/node_modules/ts-node/src/index.ts:517:19) 在 getOutput (/home/mycomputer/MyTrailProjects/typescript-rest-shop/node_modules/ts-node/src/index.ts:752:36) 在 Object.compile (/home/mycomputer/MyTrailProjects/typescript-rest-shop/node_modules/ts-node/src/index.ts:968:32) 在 Module.m._compile (/home/mycomputer/MyTrailProjects/typescript-rest-shop/node_modules/ts-node/src/index.ts:1056:42) 在 Module._extensions..js (internal/modules/cjs/loader.js:789:10) 在 Object.require.extensions.(anonymous function) [as .ts] (/home/mycomputer/MyTrailProjects/typescript-rest-shop/node_modules/ts-node/src/index.ts:1059:12) 在 Module.load (internal/modules/cjs/loader.js:653:32) 在 tryModuleLoad (internal/modules/cjs/loader.js:593:12) 在 Function.Module._load (internal/modules/cjs/loader.js:585:3)