-
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
ThunkAction is not assignable to parameter of type 'AnyAction'. #333
Comments
Do you actually have the store configured to use the thunk middleware? It would really help to see a CodeSandbox or a repo that shows this problem happening. |
I will create a project. But is really simple to reproduce, you just install the |
@mendesbarreto unfortunately I don't know what the |
@mendesbarreto maybe related |
For me
I ended up using an empty type-import to clue Webpack into the fact I wasn't trying to import a JS module: import type {} from 'redux-thunk/extend-redux'; |
Got the same issue, only for react-redux v8. Would love to have a fix for this. |
react-redux v8 was where I was running into the issue as well. Are you importing |
I'll repeat my usual refrain :) REPROS! WE NEED REPROS! We really need actual projects that show the error happening so I can investigate. |
FWIW, I believe the reason this is coming up as an issue with react-redux v8 is because the v7 // NOTE: the first overload below and note above can be removed if redux-thunk typings add an overload for
// the Dispatch function (see also this PR: https://github.com/reduxjs/redux-thunk/pull/247)
export function useDispatch<TDispatch = Dispatch<any>>(): TDispatch;
export function useDispatch<A extends Action = AnyAction>(): Dispatch<A>; The first function overload means you can dispatch literally anything (as a workaround for But in v8 it's now typed as: export declare const useDispatch: <AppDispatch extends Dispatch<AnyAction> = Dispatch<AnyAction>>() => AppDispatch; which only allows for So this issue isn't "new" it's just been masked by the loose types in the old That's just background for why this is coming up now. If you're hitting this error now as a user you should either:
|
Huh, good eye! I suppose I accidentally "fixed" this while doing some cleanup on the types :) |
@markerikson Here's your repro. :) The line I linked to errors with Webpack because it's trying to import |
Heh, thanks :) So backing up, it sounds like there's two different "issues" going on here:
Is there anything else going on atm beyond those? Seems like we could consider this resolved if not. |
@markerikson Thanks for looking into this. I am at work but I'll see if I can reproduce the bug locally later. The bug occurred to me in a project where I don't use the toolkit but the old redux action/reducer pattern instead. |
@Methuselah96 I was just importing directly |
@allicanseenow Yeah, an import from |
@markerikson Yeah, that sounds right. Although on your first point, for vanilla Redux users the TypeScript guide does not mention the need to import |
In theory, our advice of |
Yeah if the middleware is the only enhancer it might work, but I don't think composing multiple enhancers with the Redux types works right now without reduxjs/redux#3776 (can we get that merged :)). I haven't tested it out either, so maybe it would work. |
Can confirm that |
Huh, interesting. We do have a bunch of custom type-level code in |
It should, but I don't think people rely on |
I'm using Redux Toolkit and upgrading the package version to at least 1.7.0 solved this issue for me.
|
@Methuselah96's solution pointed me in the right direction but since // This is required to fix redux thunk errors introduced with react-redux version 8
import 'redux'
declare module 'redux' {
/**
* Overload for bindActionCreators redux function, returns expects responses
* from thunk actions
*/
function bindActionCreators<
ActionCreators extends ActionCreatorsMapObject<any>
>(
actionCreators: ActionCreators,
dispatch: Dispatch
): {
[ActionCreatorName in keyof ActionCreators]: ReturnType<
ActionCreators[ActionCreatorName]
> extends ThunkAction<any, any, any, any>
? (
...args: Parameters<ActionCreators[ActionCreatorName]>
) => ReturnType<ReturnType<ActionCreators[ActionCreatorName]>>
: ActionCreators[ActionCreatorName]
}
/*
* Overload to add thunk support to Redux's dispatch() function.
* Useful for react-redux or any other library which could use this type.
*/
export interface Dispatch<A extends Action = AnyAction> {
<ReturnType = any, State = any, ExtraThunkArg = any>(
thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, A>
): ReturnType
}
}
|
Today I ran into the same problem when I wanted to @reduxjs/toolkit: 1.8.2 |
How are you dispatching it? If you're using |
@Methuselah96 Yes, I am using typed export type { RootStore, AppDispatch };
export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootStore> = useSelector;
export default store; function useDispatchSafeDealAction() {
const dispatch = useAppDispatch();
const dispatchAction = React.useCallback(() => {
dispatch(OAuthAPI.endpoints.getOAuthToken.initiate());
}, [dispatch]);
return [dispatchSafeDealAction];
} Typescript error: |
@sevgeek : can you show your actual store setup code, preferably as a CodeSandbox or Github repo? |
@markerikson Yes, sure. I use for configure store very simple reducer, my API slice reducer and additional listener middleware. I think I found a script to reproduce the error. When I disable the connection of my When I return connection my |
@sevgeek : I think the issue here is the use of If I uncomment that API slice reducer, and then remove the square brackets from both the We do some complex types manipulation to figure out how middleware might alter the type of |
@markerikson I transferred the pluggable middlewares from |
* Typing actions with ThunkDispatach * Adding ts fix recommended by reduxjs/redux-thunk#333 * rename type and clear double declare global for Window Co-authored-by: stefano <stefanoacosta.92@gmail.com>
Forgot to comment. I was able to resolve the issue globally in my personal project by importing 'redux-thunk/extend-redux', as suggested by @Methuselah96 I created a custom file
And I include it in
|
This comment was marked as duplicate.
This comment was marked as duplicate.
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import { AnyAction, combineReducers } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
const rootReducers = combineReducers();
type AppState = ReturnType<typeof rootReducers>;
type TypedDispatch<T> = ThunkDispatch<T, any, AnyAction>;
export const useAppDispatch = () => useDispatch<TypedDispatch<AppState>>();
export const useAppSelector: TypedUseSelectorHook<AppState> = useSelector;
// USE
const dispatch = useAppDispatch();
const state = useAppSelector((state: AppState) => state.xxx); |
it worked just fine! thank you very much! |
Hi, I could do The type of store is: // without type casting
const store: Store<State, Action<any>> & {
dispatch: unknown;
}
store.dispatch(testGetState()); // complained like `Argument of type 'ThunkResult<void>' is not assignable to parameter of type 'Action<any>'.ts(2345)`... :(
// with type casting
const store: Store<State, Action<any>> & {
dispatch: ThunkDispatch<State, undefined, Actions>;
}
store.dispatch(testGetState()); // working! :) |
@bokusunny : you really shouldn't ever use the TS The right answer here is to use Redux Toolkit's |
How can i make my own magical dispatch that injects stuff:
It's complaining it can't assign |
@pailhead : a few different issues with that:
|
I don't want to use |
@pailhead I'm not asking about "why" you're trying to do this - I'm telling you how to fix it :) |
|
@pailhead I know. Please follow the instructions I gave you to fix that TS error! |
I'm going around in circles typing as
Ie. this works, but |
Assuming you're using Redux Toolkit and following our store setup that I linked,, Then, you need to use that type for this new "override dispatch" function you're returning: const moduleDispatch = useMemo(() => {
const stableMeta = { modulePath }
const mySpecialDispatch: AppDispatch = (action) => {
action.meta = stableMeta
return dispatch(action)
}
}, [dispatch, modulePath]) and definitely do not include So:
|
@pailhead : well, it should :) Honestly, using this issue thread as a chat support system doesn't work well. If you're still having issues, please come by the The types and approach I told you will work, but it's also possible that there's something odd about your specific project setup and configuration that we don't know about. |
Bug Report
Package name / version
redux-thunk@2.4.1
Description
I am trying to use a simple method from my store:
It is working on run time, but I am receiving an error message on Typescript:
I already tried to import the extend-redux like that:
But the problem is the run time does not find file =S.
I already tried every single solution that's I found on the previous issues, but non of them worked for me.
Steps to reproduce
store.dispatch(ThunkAction)
Expected behavior
No error message from typescript when using
store.dispatch(ThunkAction)
Environment
The text was updated successfully, but these errors were encountered: