Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
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