I'm currently working with Redux and typesafe-actions, and I am trying to find a way to automatically generate types for the actions in my reducer. Specifically, I want to have code completion for each of the string literal values of the action.type property.
Here's my setup so far:
I've defined my actions using typesafe-actions as follows:
export const increment = (amount: number) => action("INCREMENT", { amount });
export const decrement = (amount: number) => action("DECREMENT", { amount });
To get the desired type, I use this line of code:
export type CounterActions = ReturnType<typeof increment | typeof decrement>;
However, maintaining this can be cumbersome because every time I add a new action, I need to remember to update the CounterActions type.
My solution is to store all action creator functions in an array like this:
export const actions = [
(amount: number) => action("INCREMENT", { amount }),
(amount: number) => action("DECREMENT", { amount })
]
Now, I am trying to figure out how to incorporate the contents of the actions array into the CounterActions type. Any suggestions on how to achieve this?