Whenever we need to add a new action to our react/redux application, we copy a lot of boilerplate code, I'd like a helper to try and speed up this process, whilst this is a simple example, it's basically all we'll need to automate.
We create an Action interface, Payload interface, the action payload will need to be exported somehow, and the "action" function as well.
interface Payload {
something: {
[key: string]: boolean;
}
}
export interface Action {
type: 'UniqueRequestKey';
payload: Payload
}
export const someAction = (payload: Payload): Action => ({
type: 'UniqueRequestKey',
payload
});
The above is what we're currently doing, but i feel like a helper method could make this a lot simpler as it's a lot of repetitive code, we're doing this over and over and over again.
I'd like something simple like the following, doesn't have to be exact at all!
const [someAction, Action??] = createAction<'UniqueRequestKey', {
something: {
[key: string]: boolean;
}
}>();
I know the above isn't correct, but if someone could steer me in the right direction that would be great, I've attempted this with the following but it clearly is wrong
type Combine<T, P> = {
type: T,
payload: P
};
function createAction<T,P>(): [(payload: P):Combine<T,P> => {}, type: T] {
return [(payload: P): Combine<T,P> => ({
type,
payload
}), type];
}