最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript error TS1005: ':' expected. with Object.assign() - Stack Overflow

programmeradmin5浏览0评论

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 plain few characters after?

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 plain few characters after?

Share Improve this question asked Aug 2, 2016 at 12:39 user3091275user3091275 1,0232 gold badges12 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

When using a variable as a property name in an object declaration, you need to use puted 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
    })
})
发布评论

评论列表(0)

  1. 暂无评论