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

I have a nested Object.assign()in typescript:

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

This gives the following 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.

It is strange that the errors disappear if I correct the key, for example:

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

This left me dumb, why not call action.item_idin a place where he doesn't complain about a few characters after?

+4
source share
1 answer

When using a variable as the name of a property in an object declaration, you need to use the computed property by placing it in brackets:

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

Source: https://habr.com/ru/post/1649903/


All Articles