我可以在angular + ngrx中使用对象传播语法吗?

问题描述:

我从此处阅读有关对象传播语法的信息,在尝试在我的项目中使用它时,我的设置如下:

I was reading from here about the object spread syntax and I'm trying to use it in my project, my setup is the following:

  • 角2
  • angular/cli 1.0.0-rc.0
  • ngrx/core 1.2.0
  • ngrx/商店2.2.1
  • rxjs 5.1.0
  • 打字稿2.0.10

在我的reducer.ts中,

In my reducer.ts I have

export interface State {
  [id: number]: string
}

export function reducer(state= {}, action: Action): State {
case 'TEST':
  return {
    ...state,
    2: 'foo'
  }
}

但是我遇到了以下编译错误,我试图找出问题所在:

But I got the following compiling error, I'm trying to figure out what's wrong:

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版本.

Not with the version of TypeScript that you are using.

引入了对对象属性传播语法的支持在TypeScript 2.1中:

对象传播和休息

TypeScript 2.1带来了对 ES2017扩展和休息的支持.

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 };