Skip to content

Instantly share code, notes, and snippets.

@bpander
Created August 22, 2019 14:47
Show Gist options
  • Select an option

  • Save bpander/b7110aec30d7b067fdc671f11bfe0df7 to your computer and use it in GitHub Desktop.

Select an option

Save bpander/b7110aec30d7b067fdc671f11bfe0df7 to your computer and use it in GitHub Desktop.
import { Reducer, Action } from 'redux';
interface CounterState { count: number }
const initialState: CounterState = { count: 0 };
const ADD = 'ADD';
interface AddAction extends Action<typeof ADD> {
payload: { amt: number };
}
export const addAction = (payload: AddAction['payload']): AddAction => ({
type: ADD, payload,
});
const SUBTRACT = 'SUBTRACT';
interface SubtractAction extends Action<typeof SUBTRACT> {
payload: { howMany: number };
}
export const subtractAction = (payload: SubtractAction['payload']): SubtractAction => ({
type: SUBTRACT, payload,
});
type CounterAction =
| AddAction
| SubtractAction
export const counterReducer: Reducer<CounterState, CounterAction> = (s = initialState, action) => {
switch (action.type) {
case ADD: return { ...s, count: s.count + action.payload.amt };
case SUBTRACT: return { ...s, count: s.count - action.payload.howMany };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment