Last active
April 27, 2017 14:52
-
-
Save damaon/884dda839ad34a69bfff29981e4868d0 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
| type ReduxAction = { type: string }; | |
| type Reducer<T> = (T, ReduxAction) => T; | |
| type ReducerDefinition<T> = { [ACTION_TYPE: string]: (ReduxAction) => (T) => T }; | |
| export function createReducer<T>( | |
| initialState: T, | |
| definition: ReducerDefinition<T> | |
| ): Reducer<T> { | |
| return (state: T = initialState, action: ReduxAction): T => { | |
| const actionResponse = definition[action.type]; | |
| return actionResponse ? actionResponse(action)(state) : state; | |
| }; | |
| } | |
| type Foo = {| foo: string |}; | |
| const init: Foo = { foo: "foo" }; | |
| const reducer = createReducer( | |
| init, | |
| { | |
| "TEST": function(action){ | |
| return function(state) { | |
| state.foo; // <- Error! property foo not found in state literal | |
| return { bar: "bar" }; // <- no error. I expect error as init of Foo type doesn't contain bar property. | |
| }; | |
| }, | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok because inference works backward also, the error messages are confusing but they do show up and all that is needed to make it correct is to simply explicitly define type of reducer.