esamattis / immer-reducer

Type-safe and terse reducers with Typescript for React Hooks and Redux
http://npm.im/immer-reducer
MIT License
225 stars 15 forks source link

RFD: Don't add parameters to payload if they are not defined in the function definition #29

Closed Secretmapper closed 5 years ago

Secretmapper commented 5 years ago

Right now, parameters to action creators are added to the redux payload regardless of the action creator definition.

If we have a method in the immer reducer like so:

myMethod () {
   ...
}

And we call actions.myMethod({ myObject: true }), { myObject: true } will be in the action generated.

This can be problematic if the action creators are being passed to method handlers, or otherwise (i.e. map, parseInt, etc). For example, onClick={props.myMethod} will end up passing the event object to the redux action, which is slow as redux ends up serialising the event object. This is easy to fix in user land (just make sure you're not passing the method) though I think this is a common enough use case that it should be handled, but eager to know if you think this should be addressed in the library.

Function types with 'unused parameters' are valid typescript (though unsure if true in strict mode):

type FN = (red: boolean) => void
const a: FN = (red: boolean) => {} // valid
const b: FN = () => {} // valid
const c: FN = (red: boolean, b: boolean) => {} // not valid
esamattis commented 5 years ago

Yeah, I think it's valid in strict mode too. It should be possible to fix this by checking the size of the .length method of the generated action creator.

esamattis commented 5 years ago

Fixed in the 0.7.3 release.