Skip to content

Typescript fail with ThunkArg #4885

@MrMamen

Description

@MrMamen

The following wrapper code works in 2.6.0, but fails in 2.6.1

export type AppDispatch = typeof store.dispatch;
export const dispatchAndHandleResult = async <
  Returned, // The type of the return value of the thunk
  ThunkArg // The type of the argument the thunk receives
>(
  dispatch: AppDispatch,
  action: AsyncThunk<
    Returned,
    ThunkArg,
    { state: RootState; dispatch: AppDispatch; rejectValue: string }
  >,
  args: ThunkArg,
  onSuccess?: (result: Returned) => void,
  onFailure?: (error?: string) => void
) => {
  const actionResult = await dispatch(action(args));
  if (isFulfilled(actionResult)) {
    if (onSuccess) onSuccess(actionResult.payload);
  } else if (isRejected(actionResult)) {
    if (onFailure) onFailure(actionResult.payload);
  }
};

The reported problem from args in await dispatch(action(args));

TS2345: Argument of type 'ThunkArg' is not assignable to parameter of type 'ThunkArg & undefined'.
Type 'ThunkArg' is not assignable to type 'undefined'.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions