Last active
July 14, 2017 14:20
-
-
Save typoerr/bcbb2c8a81b100abf074844c69672a0e 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
| export interface Action<T = any, K extends keyof T = any> { | |
| type: K; | |
| payload: T[K]; | |
| } | |
| export type MapAction<T = any> = { | |
| [K in keyof T]: Action<T, K> | |
| }; | |
| export type MapActionHandler<S, A extends MapAction> = { | |
| [K in keyof A]: (state: S, payload: A[K]['payload']) => S | |
| }; | |
| export type ActionHandler<S, A> = MapActionHandler<S, MapAction<A>>; | |
| export default function createReducer<S>(initialState: S, handler: ActionHandler<any, any>) { | |
| return function reducer(state: S = initialState, action: any): S { | |
| return handler[action.type] ? handler[action.type](state, action.payload) : state; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.