import { Action , Reducer , State } from "./createStore" ; interface ReducersMapObject < S extends State = any , A extends Action = any > { [ key ] : Reducer < S , A > ; } type StateFromReducersMapObject < M > = M extends ReducersMapObject ? { [ P in keyof M ] : M [ P ] extends Reducer < infer S , any > ? S : never ; } : never ; function combineReducers < S extends State , A extends Action > ( reducers ) { const reducerKeys = Object . keys ( reducers ) ; const finalReducersObj = { } ; for ( const key of reducerKeys ) { if ( typeof reducers [ key ] === "function" ) { finalReducersObj [ key ] = reducers [ key ] ; } } return ( ( state = { } , action ) => { let hasChanged = false ; const nextState = { } ; for ( const key of reducerKeys ) { const prevStateForKey = state [ key ] ; const nextStateForKey = finalReducersObj [ key ] ( prevStateForKey , action ) ; nextState [ key ] = nextStateForKey ; hasChanged = hasChanged || nextStateForKey !== prevStateForKey ; } return hasChanged ? nextState : state ; } ) as any as Reducer < S , A > ; } export default combineReducers ;