简体   繁体   中英

spread operator giving issue with typescript

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

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 🌹

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM