-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
Return type of dispatch is unknown in beta #1705
Comments
Since we did not change anything around those types in RTK, could this be related to the bump of the thunk version @markerikson ? |
That was my thought, yes, especially since when I hover over Not sure where the |
I think this may be caused by these lines: https://github.com/reduxjs/redux-thunk/blob/ce76464960d5f1236460352fd3f2454e930f3665/src/types.ts#L30-L32 const dispatch: ThunkDispatch<{}, {}, AnyAction>
const v = dispatch({
type: 'abc'
})
typeof v // => unknown But if I add this line to <Action extends BasicAction>(action: Action): Action Now looks like this: export interface ThunkDispatch<State, ExtraThunkArg, BasicAction extends Action>
extends Dispatch<BasicAction> {
<ReturnType>(
thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>
): ReturnType
<Action extends BasicAction>(action: Action): Action
<ReturnType, Action extends BasicAction>(
action: Action | ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>
): Action | ReturnType
} it works: typeof v // => { type: string } I don't know why, and I found a more strange solution: // rtk/configureStore.ts
export interface EnhancedStore<
S = any,
A extends Action = AnyAction,
M extends Middlewares<S> = Middlewares<S>
> extends Store<S, A> {
// from:
dispatch: DispatchForMiddlewares<M> & Dispatch<A>
// to:
dispatch: Dispatch<A> & DispatchForMiddlewares<M>
} After I experimented, I found this also works. |
Interestingly, it seems like the "dispatch a thunk" aspect of the types is working fine here: const dispatchResult = store.dispatch((dispatch, getState) => 42)
// hover dispatchResult: 'number' it's the "dispatched a plain action" case that's returning FWIW, the |
This might even be a problem with In that same sandbox, if I do |
Hmm. A basic const store: EnhancedStore<{
counter: {
value: number;
};
}, AnyAction, [ThunkMiddleware<{
counter: {
value: number;
};
}, AnyAction, null> | ThunkMiddleware<{
counter: {
value: number;
};
}, AnyAction, undefined>]> That seems.... sketchy? Why would there be two different Oh. Maybe it comes from here? export type ThunkMiddlewareFor<
S,
O extends GetDefaultMiddlewareOptions = {}
> = O extends {
thunk: false
}
? never
: O extends { thunk: { extraArgument: infer E } }
? ThunkMiddleware<S, AnyAction, E>
:
| ThunkMiddleware<S, AnyAction, null> //The ThunkMiddleware with a `null` ExtraArgument is here to provide backwards-compatibility.
| ThunkMiddleware<S, AnyAction> |
I can confirm that if I add a couple additional typetests for this case in |
It does seem like ordering of these overloads is very fragile. Swapping them back and forth makes several of the thunk typetests fail depending on how they're ordered. |
Huh. I tried removing the hand-edits to With that order swapped, the type of const dispatch: Dispatch<AnyAction> & ThunkDispatch<{
counter: {
value: number;
};
}, null, AnyAction> & ThunkDispatch<{
counter: {
value: number;
};
}, undefined, AnyAction> My guess is that the order of the intersections relates to the order of overloads being compared. If I think I'm seeing the same Given that, I think I'm going to publish both the tweak to the thunk types, and the tweak to |
Just published https://github.com/reduxjs/redux-thunk/releases/tag/v2.4.1 containing the additional |
And also just merged #1772 which should fix it on the RTK side as well, and that will go out in the next 1.7 beta (or final). |
See https://codesandbox.io/s/reverent-firefly-ru026?file=/src/index.tsx
With
@reduxjs/toolkit
1.6.2, the type error goes away as the type ofdispatchResult
is inferred correctly. With 1.7.0-beta.x, it'sunknown
.The text was updated successfully, but these errors were encountered: