简体   繁体   English

打字稿错误TS1005:':'预期。使用Object.assign()

[英]Typescript error TS1005: ':' expected. with Object.assign()

I have nested Object.assign() in typescript: 我在typescript中嵌套了Object.assign()

(<any>Object).assign({}, state, {
    action.item_id: (<any>Object).assign({}, state[action.item_id], {
        label_value: action.value
    })
})

This yields those errors: 这会产生这些错误:

ERROR in ./src/reducers/ItemsReducer.ts
(2,19): error TS1005: ':' expected.

ERROR in ./src/reducers/ItemsReducer.ts
(2,26): error TS1005: ',' expected.

ERROR in ./src/reducers/ItemsReducer.ts
(2,28): error TS1136: Property assignment expected.

The weird thing is that the errors vanish if I fix the key eg: 奇怪的是,如果我修复了密钥,错误就会消失,例如:

(<any>Object).assign({}, state, {
    "fixed_key": (<any>Object).assign({}, state[action.item_id], {
        label_value: action.value
    })
})

This left me clueless, why isn't it ok to call action.item_id at that place when he doesn't complain few characters after? 这让我一无所知,为什么在那个地方没有抱怨几个字符的action.item_id在那个地方调用action.item_id是不是可以的呢?

When using a variable as a property name in an object declaration, you need to use computed property notation by putting it in brackets: 在对象声明中使用变量作为属性名称时,需要将计算属性表示法放在括号中:

(<any>Object).assign({}, state, {
    [action.item_id]: (<any>Object).assign({}, state[action.item_id], {
        label_value: action.value
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿错误TS1005:&#39;;&#39; 预期。 TS v1.8.10 - Typescript error TS1005: ';' expected. TS v1.8.10 数组中的散布操作出错。 TS1005:“,”应为。 打字稿 - Error with spread operation in array. TS1005: ',' expected. TypeScript 打字稿错误 TS1005: &#39;;&#39; 预期 - Typescript error TS1005: ';' expected 将Angular2-rc1升级到Angular2-rc2,面向TypeScript&#39;,&#39;。 (TS1005) - Upgrade Angular2-rc1 to Angular2-rc2, facing TypeScript ',' expected. (TS1005) 错误 TS1005: ',' expected, failing to compile angular 6 project - error TS1005: ',' expected, failing to compile angular 6 project TS1005 错误,“,”预期为 simple.forEach 循环 - TS1005 error, ','expected with simple .forEach loop 错误:src/app/components/button/button.component.ts:11:27 - 错误 TS1005: &#39;(&#39; 预期 - Error: src/app/components/button/button.component.ts:11:27 - error TS1005: '(' expected 为什么我得到 ':' expected.ts(1005) 作为 Typescript 错误? - Why do I get ':' expected.ts(1005) as a Typescript error? Object.assign未按预期工作 - Object.assign not working as expected 当我在 discord 中创建机器人时,为什么我在 Visual Studio Code 中得到“','预期的 ts1005” - Why i get “ ',' expected ts1005” in Visual Studio Code when i am creating a bot in discord
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM