1

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

  • 角度 2
  • 角度/cli 1.0.0-rc.0
  • ngrx/核心 1.2.0
  • ngrx/商店 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'

有任何想法吗?谢谢!

4

1 回答 1

1

不适用于您使用的 TypeScript 版本。

TypeScript 2.1 中引入了对对象属性扩展语法的支持:

对象传播和休息

TypeScript 2.1 带来了对ES2017 Spread 和 Rest的支持。

与数组传播类似,传播对象可以很方便地获得浅拷贝:

let copy = { ...original };
于 2017-04-05T07:53:36.137 回答