-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
#2979 Add strict type inference overload for combineReducers. #3484
#2979 Add strict type inference overload for combineReducers. #3484
Conversation
Shakeskeyboarde
commented
Jul 28, 2019
- Strictly infers state shape and actions from the reducers object map.
- Updated tests and comments to match new strict type inference.
- Tested and working in typescript 2.8.
…pe and actions from the reducers object map.
Deploy preview for redux-docs ready! Built with commit e26cb19 |
This is a fix for issue #2979. |
This is basically what #3411 was doing, but without the BC breakage. I think this is good, but if others want to chime in, feel free. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies 😊. Habit from personal projects. Thanks for the heads up and fix.
BTW, we're testing against 3.5.3 now, so compatibility with older versions isn't really of concern. I've found most people upgrade TS pretty regularly. |
Should we be encouraging shapes of reducers that include any type other than For example, suppose we have this seemingly innocuous reducer: type CounterAction = IncrementAction | DecrementAction
type IncrementAction = {type: "increment", amount: number}
type DecrementAction = {type: "decrement", amount: number}
function counterReducer(state: number = 0, action: CounterAction): number {
if(action.type === "increment") {
return state + action.amount;
} else {
return state - action.amount;
}
} The This particular reducer produces a state of |
@pfgray The intention here was to make the combined reducer consistent with the typing of the individual reducers. You're right that maybe individual reducers should use But, if you do use more specific types for your reducer action parameters, then currently that typing is stripped by combining. Personally, I do specify types for my actions. But, I do it knowing I can't use an implicit else/default in my reducer. I add the types for testing (so I know when I'm testing an expected action), and for action property types when writing the code that consumes the action. |
I think we should make that implicit assumption explicit in the type system! That way less astute developers (like myself!) don't get tripped up. I think the right approach would be to remove the first signature of |
So, aside from the AnyAction discussion, which is valid but better suited for another issue/PR, I think we're good here. Let's merge this in. |