deox icon indicating copy to clipboard operation
deox copied to clipboard

handleAction allows you to return a state object with any additional properties

Open dannypule opened this issue 4 years ago • 3 comments

This should fail because otherNum is not part of the State interface:

interface State {
  num: number
}

export const initialState: State = {
  num: 0
};

export const appsReducer = createReducer(initialState, handleAction => [
  handleAction(actions.getApps.success, () => ({
    num: 0,
    otherNum: 0 // adding this property should trigger a warning
  })),
]);

Typescript only displays a warning when the return type of State is explicitly defined but it would be nice to have it happen automatically

export const appsReducer = createReducer(initialState, handleAction => [
  handleAction(actions.getApps.success, (): State => ({ // specify the return type
    num: 0,
    otherNum: 0 // this triggers a warning correctly because we added `State` as the return type
  })),
]);

dannypule avatar Oct 04 '19 16:10 dannypule

Unfortunately, it's not that simple to get rid of additional properties in TypeScript but I think it's not impossible.

the-dr-lazy avatar Oct 09 '19 05:10 the-dr-lazy

I was very surprised to see in our codebase things being returned, that shouldn't have been. I guess it's not a major issue, but it does cause some confusion. Do you know how to solve it @the-dr-lazy?

DCzajkowski avatar Oct 24 '20 23:10 DCzajkowski

There are some playgrounds (especially last playground) in #122 which I have done such thing.

the-dr-lazy avatar Oct 27 '20 17:10 the-dr-lazy