spread operator giving issue with typescript

2019-08-20 10:59发布

I am trying to implement a redux store for my react-typescript app. I am having a problem in my reducer. In native react-apps I did the following

  reminders = [...state, reminder(action)];
  return reminders;

the spread operator works perfectly. and the new object is added to array immutably.

with typescript this is not happening.(get an empty object instead of an array) I tried object.assign

  return (<any>Object).assign({}, state, reminder(action));

This replaces the current object rather then adding it to the array and I dont think its doing it in an immutable way.

I tried uisng immutable.js and the reducer was not being called at all.

 return map([state,reminder(action)])

no idea what is wrong. also after using objext.assign the nextprops and currents props always come same. even if it is changed in the shouldContainerUpdate() method

1条回答
该账号已被封号
2楼-- · 2019-08-20 11:27

the spread operator works perfectly. and the new object is added to array immutably.

Use the exact same in TypeScript and it will work perfectly:

reminders = [...state, reminder(action)];
return reminders;

Why

Because TypeScript follows the same semantics as JavaScript for JavaScript syntax

查看更多
登录 后发表回答