the-dr-lazy / deox

Functional Type-safe Flux Standard Utilities

Home Page:https://deox.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

dannypule opened this issue · 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
  })),
]);

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

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?

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