Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to automatically generate PayloadAction type unions from an AsyncActionCreateBuilder instance to facilitate writing custom epic generic templates #212

Open
dcs3spp opened this issue Nov 20, 2019 · 0 comments

Comments

@dcs3spp
Copy link

dcs3spp commented Nov 20, 2019

Is your feature request related to a real problem or use-case?

I have written a generic epic that accepts an async action as a parameter:

export const genericHandler = <
  TRequestType extends string,
  TSuccessType extends string,
  TFailType extends string,
  TModel,
>(
  asyncAction: AsyncActionCreatorBuilder<
    [TRequestType, RequestPayload<TModel>],
    [TSuccessType, SuccessPayload<TModel>],
    [TFailType, FailurePayload<TModel>]
  >,
) => {
  const epic: Epic<
    // epic input action types union - manually specifying.....
    | PayloadAction<
        TRequestType,
        RequestPayload<TModel>,
      >
    | PayloadAction<TSuccessType, SuccessPayload<TModel>>
    | PayloadAction<TFailType, FailurePayload<TModel>>,
    // output action types union - manually specifying
    | PayloadAction<
        TRequestType,
        RequestPayload<TModel>
      >
    | PayloadAction<TSuccessType, SuccessPayload<TModel>>
    | PayloadAction<TFailType, FailurePayload<TModel>>,
    RootState,
    Services
  > = (action$, state$, { apiService }) => {
      // some source code ....
  }

The type of the aysncAction parameter is AsyncActionCreatorBuilder.
In the redux-observable Epic<...> generic I manually specify the PayloadAction types for Input and Output actions in the stream.

Describe a solution including usage in code example

Is there any way to automatically generate the PayloadAction types to reduce complexity?
Is it possible to derive the PayloadAction union types from AsyncActionCreatorBuilder to reduce complexity....

// imports for RequestPayload, SuccessPayload, FailurePayload etc.
export const customEpicCreator_A = <
  TRequestType extends string,
  TSuccessType extends string,
  TFailType extends string,
  TModel,
>(
  asyncAction: AsyncActionCreatorBuilder<
    [TRequestType, RequestPayload<TModel>],
    [TSuccessType, SuccessPayload<TModel>],
    [TFailType, FailurePayload<TModel>]
  >,
) => {
  const epic: Epic<
    // Payload type union automatically generated for input and output types from AsyncActionCreatorBuilder object
    GeneratePayloadActionTypes<asyncAction>,
    GeneratePayloadActionType<asyncAction>,
    RootState,
    Services
  > = (action$, state$, { apiService }) => {
      // some source code ....
  }

export const customEpicCreator_B = <
  TRequestType extends string,
  TSuccessType extends string,
  TFailType extends string,
  DifferentRequestObject,
  DifferentSuccessObject,
  DifferentFailObject,
>(
  asyncAction: AsyncActionCreatorBuilder<
    [TRequestType, DifferentRequestObject],
    [TSuccessType, DifferentSuccessObject],
    [TFailType, DifferentFailObject]
  >,
) => {
  const epic: Epic<
    // Payload type union automatically generated for input and output types from AsyncActionCreatorBuilder object
    GeneratePayloadActionTypes<asyncAction>,
    GeneratePayloadActionType<asyncAction>,
    RootState,
    Services
  > = (action$, state$, { apiService }) => {
      // some source code ....
  }

Who does this impact? Who is this for?

Typescript users

Describe alternatives you've considered (optional)

I tried using ActionType<typeof asyncAction> but this generates type

asyncAction: {
    request: PayloadActionCreator<TRequestType, RequestPayload<TModel>>;
    success: PayloadActionCreator<TSuccessType, SuccessPayload<TModel>>;
    failure: PayloadActionCreator<TFailType, FailPayload<TModel>>;
} 

I think this is because I am passing in the parameter type as an AsyncActionCreatorBuilder.

Additional context (optional)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants