spread operator giving issue with typescript

落花浮王杯 提交于 2019-12-13 03:36:38

问题


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:


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 🌹



来源:https://stackoverflow.com/questions/47298885/spread-operator-giving-issue-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!