[英]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.