Created
February 11, 2019 23:46
-
-
Save bloadvenro/3ac411dce00cf68bd6ed4c3b47e0e09f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Dispatch<A extends { type: string }> { | |
(action: A): void; | |
} | |
interface StateGetter<S extends { [key: string]: any }> { | |
(): S; | |
} | |
interface ActionReducer<A extends { type: string }, S extends { [key: string]: any }> { | |
dispatch: Dispatch<A>; | |
getState: StateGetter<S>; | |
} | |
function ActionReducer<A extends { type: string }, S extends { [key: string]: any }>( | |
reducer: Reducer<S, A>, | |
initialState: S, | |
): ActionReducer<A, S> { | |
let state = initialState; | |
return { | |
dispatch(action) { | |
state = reducer(state, action); | |
}, | |
getState: () => ({ ...state }), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment