Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have nested Object.assign() in typescript:

(<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 e.g.:

(<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?

like image 603
user3091275 Avatar asked Aug 02 '16 12:08

user3091275


1 Answers

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
    })
})
like image 156
JohnnyHK Avatar answered Oct 25 '22 19:10

JohnnyHK