Skip to content

Instantly share code, notes, and snippets.

@quicksnap
Created January 15, 2018 23:28
Show Gist options
  • Save quicksnap/be1e0d1e4d6ebbe59e41ee7f1b3ddb6d to your computer and use it in GitHub Desktop.
Save quicksnap/be1e0d1e4d6ebbe59e41ee7f1b3ddb6d to your computer and use it in GitHub Desktop.
// For this example, we use `any` a lot. We will fully type this soon
// This code is a little dense, but serves as a good abstraction
function makeAction(type: string) {
return (fn: (...args: any[]) => any) => {
const returnFn = (...args: any[]) => ({ ...fn(...args), type })
(returnFn as any).__actionType = type;
return returnFn;
};
}
// Usage:
const addToCart = makeAction('ADD_TO_CART')((productIds: string[]) => ({
payload: {
productIds: productIds,
timeAdded: new Date(),
},
}));
// Dispatch:
myStore.dispatch(addToCart(['ABC123', 'DEFG56']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment