-
-
Notifications
You must be signed in to change notification settings - Fork 98
Closed
Description
I was playing a bit with providing additional createAction methods which have a very specific shape and came up with the following for e.g. actions with a payload:
const createWithPayload = <P, T extends string>(typeString: T): ((payload: P) => PayloadAction<T, P> & TypeGetter<T>) => {
const f = createAction(typeString, (payload: P) => ({ type: typeString, payload }));
return f;
};
which you could call like this:
const a = createWithPayload<string, "CHANGE_COMMENT">("CHANGE_COMMENT");
You can push this even further:
const createWithPayload2 = <T extends string>(
typeString: T
): (<P>() => ((payload: P) => PayloadAction<T, P> & TypeGetter<T>)) => {
return <P>() => {
const f = createAction(typeString, (payload: P) => ({ type: typeString, payload }));
return f;
};
};
And you call it like this:
const a = createWithPayload2("CHANGE_COMMENT")<string>();
// instead of:
const a = createAction("CHANGE_COMMENT", (payload: string) => ({
type: "CHANGE_COMMENT",
payload
}));
What do you think?
Metadata
Metadata
Assignees
Labels
No labels