Typescript错误TS1005:':'预计。与Object.assign()

Typescript错误TS1005:':'预计。与Object.assign()

问题描述:

我在打字稿嵌套Object.assign()Typescript错误TS1005:':'预计。与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. 

奇怪的是,如果我解决的关键例如错误消失:

(<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 
    }) 
}) 
+0

谢谢! :)它现在像一种魅力。 – user3091275