Description
ThunkDispatch
has two overloads:
export interface ThunkDispatch<S, E, A extends Action> {
<T extends A>(action: T): T;
<R>(asyncAction: ThunkAction<R, S, E, A>): R;
}
This works fine if I'm calling dispatch(xyz)
where xyz
has either type T
or type ThunkAction<...>
, as is normally the case with action creators, as they can declare what type they return. However, I have an interface where users of that interface need to provide an action creator. And the caller of that action creator doesn't care which of the two kinds of action is returned, it just passes that action to dispatch()
. But there is no single type that captures "whatever one can pass to dispatch()". So currently I'm having the function return T | ThunkAction<...>
. But that's impossible to pass to a ThunkDispatch
function, because neither of the overloads accepts this union type.
The problem can be illustrated with a smaller example, and has been discussed in the TypeScript repository before (microsoft/TypeScript#14107):
interface Func {
(a: number): number;
(a: string): string;
}
function outer(func: Func, value: number | string): void {
func(value);
}
A potential solution is to add a third overload:
(a: number | string): number | string;
<R, T extends A>(action: T | ThunkAction<R, S, E, A>): T | R;
Would that solution also make sense for redux-thunk? Or is there another solution for my problem?