问题
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