Skip to content

Did you consider pushing this further towards more boilerplate-saving? #12

@flq

Description

@flq

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions