ThunkAction is not assignable to parameter of type 'AnyAction'.

See original GitHub issue

Bug Report

Package name / version

redux-thunk@2.4.1

Description

I am trying to use a simple method from my store:

store.dispatch(ThunkAction);

It is working on run time, but I am receiving an error message on Typescript:

TS2345: Argument of type 'ThunkAction<Promise<AuthorizationDto | null>, State, unknown, SetAuthorizationAction>' is not assignable to parameter of type 'AnyAction'.   Property 'type' is missing in type 'ThunkAction<Promise<AuthorizationDto | null>, State, unknown, SetAuthorizationAction>' but required in type 'AnyAction'. 

I already tried to import the extend-redux like that:

import 'redux-thunk/extend-redux';

But the problem is the run time does not find file =S.

Screen Shot 2022-04-13 at 3 36 16 PM

I already tried every single solution that’s I found on the previous issues, but non of them worked for me.

Steps to reproduce

  • Install the redux-thunk using “npm install”
  • Create a ThunkAction
  • Call the store.dispatch(ThunkAction)
  • See typescript Type error

Expected behavior

No error message from typescript when using store.dispatch(ThunkAction)

Environment

  • OS: macOS Monterey 12.3.1
  • Node/npm version: node v16.14.2 / npm 8.5
  • Platform: React Native iOS/Android

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:16
  • Comments:34 (19 by maintainers)

github_iconTop GitHub Comments

41reactions
Methuselah96commented, Apr 26, 2022

FWIW, I believe the reason this is coming up as an issue with react-redux v8 is because the v7 useDispatch() was typed as:

// 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 redux-thunk).

But in v8 it’s now typed as:

export declare const useDispatch: <AppDispatch extends Dispatch<AnyAction> = Dispatch<AnyAction>>() => AppDispatch;

which only allows for Action objects.

So this issue isn’t “new” it’s just been masked by the loose types in the old react-redux type definitions.


That’s just background for why this is coming up now. If you’re hitting this error now as a user you should either:

  • Use Redux Toolkit and define typed hooks (which will be correctly typed and allow thunk actions)
  • If you’re using vanilla Redux, most bundlers won’t like import 'redux-thunk/extend-redux' in a regular TypeScript file because they think you’re trying to import JavaScript so you can either:
    • Use import type {} from 'redux-thunk/extend-redux'
    • Put /// <reference types="redux-thunk/extend-redux" /> in a .d.ts file that’s included in your type-checking
    • Put import 'redux-thunk/extend-redux' in a .d.ts file that’s included in your type-checking
13reactions
vanzinvestorcommented, Oct 27, 2022
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);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Argument of type 'ThunkAction' is not assignable to parameter ...
Firstly: So your thunk not firing any other action can only have one meaning: The other calls to dispatch are most likely never...
Read more >
TIPS react - Is not assignable to parameter of type 'AnyAction'
Salut les curieux, les curieusesQuand on travaille avec react, redux, et le redux toolkit, on peut tomber sur des cas tricky qui peuvent ......
Read more >
How to dispatch a thunk with TS? : r/typescript - Reddit
argument of type "(dispatch: DispatchType) => void" is not assignable to parameter of type "AnyAction". The fetchAllProducts code
Read more >
How to Use Thunks with Redux Toolkit and TypeScript
If we want to use rejectWithValue we need to define return-type and payload creator argument type as well. Adding Thunks to Slices#. Now,...
Read more >
Usage With TypeScript - Redux
Since these are actual variables, not types, it's important to ... which takes the type of the action.payload field as its generic argument....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found