Skip to content

Instantly share code, notes, and snippets.

@carlrip
Last active September 20, 2019 03:21
Show Gist options
  • Save carlrip/5033765ec41cefa08a2cedf4f272f6c7 to your computer and use it in GitHub Desktop.
Save carlrip/5033765ec41cefa08a2cedf4f272f6c7 to your computer and use it in GitHub Desktop.
Type safe reducer
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