我可以在 angular + ngrx 中使用对象传播语法吗?
Can I use object spread syntax in angular + ngrx?
我从 here 那里读到有关对象传播语法的内容,我正尝试在我的项目中使用它,我的设置如下:
- angular 2
- angular/cli 1.0.0-rc.0
- ngrx/core 1.2.0
- ngrx/store 2.2.1
- rxjs 5.1.0
- 打字稿 2.0.10
在我的 reducer.ts 我有
export interface State {
[id: number]: string
}
export function reducer(state= {}, action: Action): State {
case 'TEST':
return {
...state,
2: 'foo'
}
}
但是我遇到了以下编译错误,我正在尝试找出问题所在:
Property assignment expected
Type '{ 2: string; state: State; }' is not assignable to type 'State'
Object literal may only specify known properties, and 'state' does not exist in type 'State'
有什么想法吗?谢谢!
不适用于您使用的 TypeScript 版本。
对对象属性传播语法的支持是 introduced in TypeScript 2.1:
Object Spread and Rest
TypeScript 2.1 brings support for ES2017 Spread and Rest.
Similar to array spread, spreading an object can be handy to get a shallow copy:
let copy = { ...original };
我从 here 那里读到有关对象传播语法的内容,我正尝试在我的项目中使用它,我的设置如下:
- angular 2
- angular/cli 1.0.0-rc.0
- ngrx/core 1.2.0
- ngrx/store 2.2.1
- rxjs 5.1.0
- 打字稿 2.0.10
在我的 reducer.ts 我有
export interface State {
[id: number]: string
}
export function reducer(state= {}, action: Action): State {
case 'TEST':
return {
...state,
2: 'foo'
}
}
但是我遇到了以下编译错误,我正在尝试找出问题所在:
Property assignment expected
Type '{ 2: string; state: State; }' is not assignable to type 'State'
Object literal may only specify known properties, and 'state' does not exist in type 'State'
有什么想法吗?谢谢!
不适用于您使用的 TypeScript 版本。
对对象属性传播语法的支持是 introduced in TypeScript 2.1:
Object Spread and Rest
TypeScript 2.1 brings support for ES2017 Spread and Rest.
Similar to array spread, spreading an object can be handy to get a shallow copy:
let copy = { ...original };