You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to define reducers used in redux. I don't know why this definition not works.
TypeScript Version: 2.4.1
Code
// Type definitions from 'redux'exportinterfaceAction{type: any;}exporttypeReducer<S>=<AextendsAction>(state: S,action: A)=>S;
// My codeimport{Action,Reducer}from'redux';enumTypes{add,}interfaceIncrementextendsAction{type: Types.add;value: number;}constcount: Reducer<number>=(state: number,action: Increment)=>{if(action.type===Types.add){returnstate+action.value;}returnstate;};
Expected behavior:
This should work!
Actual behavior:
(14,7): error TS2322: Type '(state: number, action: Increment) => number' is not assignable to type 'Reducer<number>'.
Types of parameters 'action' and 'action' are incompatible.
Type 'A' is not assignable to type 'Increment'.
Type 'Action' is not assignable to type 'Increment'.
Property 'value' is missing in type 'Action'.
Not exactly a dupe of that. This is due to stricter generic checks though, which that issue is related to.
You're trying to assign a (state: number, action: Increment) => number to a <A extends Action>(state: number, action: A) => number, meaning that you're expecting more than you may potentially be given. So your Reducer definition is incorrect.
Check out reduxjs/redux#2467 for the fix I put out to Redux which includes an explanation.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
zhahaoyu commentedon Jul 13, 2017
This works in Typescript 2.3.4
kitsonk commentedon Jul 13, 2017
Dupe of #16985
DanielRosenwasser commentedon Aug 28, 2017
Not exactly a dupe of that. This is due to stricter generic checks though, which that issue is related to.
You're trying to assign a
(state: number, action: Increment) => numberto a<A extends Action>(state: number, action: A) => number, meaning that you're expecting more than you may potentially be given. So yourReducerdefinition is incorrect.Check out reduxjs/redux#2467 for the fix I put out to Redux which includes an explanation.
mhegazy commentedon Sep 12, 2017
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.