我在 typescript 中嵌套了 Object.assign()
:
(<any>Object).assign({}, state, {
action.item_id: (<any>Object).assign({}, state[action.item_id], {
label_value: action.value
})
})
这会产生这些错误:
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.
奇怪的是,如果我修复了 key ,错误就会消失,例如:
(<any>Object).assign({}, state, {
"fixed_key": (<any>Object).assign({}, state[action.item_id], {
label_value: action.value
})
})
这让我一头雾水,为什么在那个地方调用 action.item_id
之后他没有提示几个字符就不行了?
最佳答案
在对象声明中使用变量作为属性名时,需要使用computed property将其放在括号中表示:
(<any>Object).assign({}, state, {
[action.item_id]: (<any>Object).assign({}, state[action.item_id], {
label_value: action.value
})
})
关于javascript - Typescript 错误 TS1005 : ':' expected. 与 Object.assign(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38720664/