redux: combineReducers is broken with TypeScript 2.6.1 and Redux 3.7.2
combineReducers is broken with TypeScript 2.6.1 and Redux 3.7.2
What is the current behavior? Here is my code
import State from './state'
import { defaultState } from './state'
import { combineReducers, Reducer } from 'redux'
import { Dispatch, Store, Action } from 'redux'
import { reducer as count } from '../feature/counter/counterScreen'
export enum keys {
INCREMENT = 'increment',
DECREMENT = 'decrement',
}
interface IncrementAction {
readonly type: keys.INCREMENT
readonly payload: {
readonly size: number
}
}
const incrementAction = (size: number): IncrementAction => ({
type: keys.INCREMENT,
payload: {
size: size,
},
})
interface DecrementAction {
readonly type: keys.DECREMENT
readonly payload: {
readonly size: number
}
}
const decrementAction = (size: number): DecrementAction => ({
type: keys.DECREMENT,
payload: {
size: size,
},
})
export type ActionTypes = IncrementAction | DecrementAction
/** REDUCER */
export function reducer(state = defaultState.count, action: ActionTypes) {
switch (action.type) {
case keys.DECREMENT:
return state - action.payload.size
case keys.INCREMENT:
return state + action.payload.size
default:
return state
}
}
const rootReducers: Reducer<State> = combineReducers({ count })
export default rootReducers
and here is error:
yarn build v0.27.5
$ yarn run clean && yarn run lint && yarn tsc --
src/app/reducers.ts(53,54): error TS2345: Argument of type '{ count: (state: number | undefined, action: ActionTypes) => number; }' is not assignable to parameter of type 'ReducersMapObject'.
Property 'count' is incompatible with index signature.
Type '(state: number | undefined, action: ActionTypes) => number' is not assignable to type 'Reducer<any>'.
Types of parameters 'action' and 'action' are incompatible.
Type 'AnyAction' is not assignable to type 'ActionTypes'.
Type 'AnyAction' is not assignable to type 'DecrementAction'.
Property 'payload' is missing in type 'AnyAction'.
What is the expected behavior? It should compile and work.
Which versions of Redux, and which browser and OS are affected by this issue? Did this work in previous versions of Redux? Redux 3.7.2 and Typescript 2.6.1
Hot fix
It works well if I change Reducer definition in index.d.ts
file:
export type Reducer<S, A extends AnyAction> = (state: S, action: A) => S;
And my reducer:
const rootReducers: Reducer<State, ActionTypes> = combineReducers({ count })
How do you think about this issue and hot fix?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 14
- Comments: 28 (5 by maintainers)
Commits related to this issue
- Add strict typing Except for functions because of https://github.com/reactjs/redux/issues/2709 — committed to metiscoderclass/rooster.hetmml.nl by nloomans 7 years ago
- Fix combineReducers typings. Related to https://github.com/reduxjs/redux/issues/2709 — committed to gilbsgilbs/redux by gilbsgilbs 4 years ago
Not very satisfying, but I’ve found that asserting the
ReducersObjectMap
toany
gets me compiling again.Using the version 4 beta typings does not work for me.
Can this be reopened? It’s still a problem. Redux 4.0.4, TypeScript 3.7.2
Try to add “any” as an alternative data type in you reducer:
export function reducer(state = defaultState.count, action: ActionTypes | any) { … }
redux 4.0.5 typescript 3.8.3
issue persists
edit: I found a solution, simply pass you store state type to combineReducers and the error goes away
@timdorr I am actually using
payload
in action props. However, I changed my code to resemble to what @lenguyenthanh posted. Still no luck.Error:
I think, after introduction to
strictFunctionTypes
switch with Typescript 2.6.1,Action<any>
and any extended interface will not match. I am not sure how @lenguyenthanh managed to compile his code with TS@2.6.1 and Redux@4.0.0-beta.1, but I could only compile by settingstrictFunctionTypes: false
intsconfig.json
.Using Typescript 3.2.4 and Redux 4.0.1, the issue remains. Used @seepel solution for now.
Thank you so much for answer. Should we keep it open for other people see it? I couldn’t search an issue for it before.
Same problem : Typescript : 3.6.3 and Redux 4.0.4
Force to cast as any, an so I don’t have any autocomplete on my mapStateToProps’ state 😦
If you want an alternative to have the autocomplete in your mapStateToProps
The IStore is just an Interface made of all my Reducers’s InitialState
it will break the typing, if you need to types store base on the rootReducer as I do here
export type RootState = ReturnType<typeof rootReducer>;
instead, this worked for me
const persistor = persistStore(store as any);
@tipng I am using this workaround without disabling
strictFunctionTypes
:I hope it helps.
These are on the
next
tag on npm right now with 4.0.0-beta.1.npm install redux@next