-
-
Save quicksnap/be1e0d1e4d6ebbe59e41ee7f1b3ddb6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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