Skip to content

Instantly share code, notes, and snippets.

@robwormald
Created July 25, 2017 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwormald/c941830216ee4d3faa484a8d6ad96388 to your computer and use it in GitHub Desktop.
Save robwormald/c941830216ee4d3faa484a8d6ad96388 to your computer and use it in GitHub Desktop.
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