piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 98 forks source link

handleAction allows return object with properties not pesent in the state type #253

Closed zdila closed 2 years ago

zdila commented 3 years ago

I am not sure if it is a bug or feature, but this compiles just OK:

const action = { type: 'action1' as const };

function createAction() {
  return action;
}

type State = { foo: number };

createReducer<State, typeof action>({ foo: 1 }).handleAction(
  createAction,
  () => ({
    foo: 2,
    bar: 3 // bar is not in State
  }),
);

If it is the feature, why handleAction allows returning object with properties not present in the state type? This way the compiler will not catch some errors.

ovidiu-balau commented 2 years ago

Is there any fix for this? It renders createReducer useless for me if handleAction cannot understand its return type

piotrwitek commented 2 years ago

This is typescript limitation, doesn't have anything to do with the library. Just provide an explicit return type and it will work as expected.