Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dannypule opened this issue Oct 4, 2019 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@dannypule
Copy link

dannypule commented Oct 4, 2019

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
  })),
]);
@the-dr-lazy the-dr-lazy added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Oct 9, 2019
@the-dr-lazy
Copy link
Owner

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

@DCzajkowski
Copy link

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?

@the-dr-lazy
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants