Last active
September 20, 2019 03:21
-
-
Save carlrip/5033765ec41cefa08a2cedf4f272f6c7 to your computer and use it in GitHub Desktop.
Type safe reducer
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 Increment = { | |
| type: 'increment'; | |
| incrementStep: number; | |
| }; | |
| type Decrement = { | |
| type: 'decrement'; | |
| decrementStep: number; | |
| }; | |
| type Actions = Increment | Decrement; | |
| const reducer = (state: State, action: Actions): State => { | |
| switch (action.type) { | |
| case 'increment': | |
| return { count: state.count + action.incrementStep }; | |
| case 'decrement': | |
| return { count: state.count - action.decrementStep }; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment