Skip to content

Instantly share code, notes, and snippets.

@robwormald
Created Jul 25, 2017
Embed
What would you like to do?
interface Action {
type: string;
}
enum UserActions {
ADD_USER = 'ADD_USER',
DELETE_USER = 'DELETE_USER'
}
interface User {
id: number;
name: string;
}
interface AddUserAction extends Action {
type: UserActions.ADD_USER,
payload: User
}
interface DeleteUserAction extends Action {
type: UserActions.DELETE_USER,
payload: number;
}
type UserAction =
AddUserAction |
DeleteUserAction
function userReducer(state: User[] = [], action: UserAction): User[] {
switch (action.type) {
case UserActions.ADD_USER:
return state.concat([action.payload]);
case UserActions.DELETE_USER:
return state.filter(user => user.id === action.payload);
default:
return state;
}
}
userReducer([], {type: UserActions.ADD_USER, payload: {id: 1, name: 'foo'}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment