我可以在angular + ngrx中使用对象传播语法吗? [英] Can I use object spread syntax in angular + ngrx?

查看:135
本文介绍了我可以在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 };

这篇关于我可以在angular + ngrx中使用对象传播语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆