-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[TypeScript] Generic action creators fail to resolve through ThunkDispatch overloads #248
Comments
See discussion in reduxjs#248 and microsoft/TypeScript#14107. Without this explicit overload, TypeScript is unable to figure out that the function can be called with an argument of type `T|ThunkAction<...>`.
* #248 Add union overload to ThunkDispatch See discussion in #248 and microsoft/TypeScript#14107. Without this explicit overload, TypeScript is unable to figure out that the function can be called with an argument of type `T|ThunkAction<...>`. * Merge ThunkDispatch union overload with renamed type parameters Co-Authored-By: Tim Dorr <timdorr@users.noreply.github.com>
I think the correct declaration is: export interface ThunkDispatch<S, E, A extends Action> {
<A>(action: A): A extends (...args: any) => infer R ? R : A;
} |
Wow that looks like magic. I'm unable to parse it in my head. Can you explain how it works with the first colon and the Do you propose this as the single overload, or as an additional one (to replace the additional one that was committed in #255? This overload uses |
in redux-thunk words,
|
Can we get a PR for that implementation instead? I agree it's probably more correct. |
I see, thanks! So at least conceptually, the parentheses are:
Can we involve
And can we bring back
And if my guess above was any good, we need to keep the outer
Actually I propose using another letter, because I can try putting this into a PR. Do you prefer code-only, or with inlined comments? I guess it depends on the readers experience with condition types whether they will understand from the code alone. |
@Yaojian If I apply your (or my modified) solution, it generally works, but there's a TypeScript error just below:
|
ThunkDispatch
has two overloads:This works fine if I'm calling
dispatch(xyz)
wherexyz
has either typeT
or typeThunkAction<...>
, 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 todispatch()
. But there is no single type that captures "whatever one can pass to dispatch()". So currently I'm having the function returnT | ThunkAction<...>
. But that's impossible to pass to aThunkDispatch
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):
A potential solution is to add a third overload:
Would that solution also make sense for redux-thunk? Or is there another solution for my problem?
The text was updated successfully, but these errors were encountered: