Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have set up a redux thunk function with typescript like this:

export const fetchActivities = (): AppThunk => async dispatch => {
    dispatch(fetchActivitiesRequest())

    try {
        const res = await fetch("/activities")
        const body = await res.json()

        dispatch(fetchActivitiesSuccess(body))
    } catch (err) {
        dispatch(fetchActivitiesError(err))
    }
}

AppThunk is simply a type deriving from the ThunkAction type, with the following type params:

export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, {}, null, Action<string>>

My issue is, that when i try to setup a unit test for my action, and when I try to dispatch the thunk action like this:

const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
const store = mockStore({ activities: [] })

store.dispatch(actions.fetchActivities())

I receive the following error message:

Argument of type 'ThunkAction<void, {}, null, Action<string>>' is not assignable to parameter of type 'AnyAction'.
  Property 'type' is missing in type 'ThunkAction<void, {}, null, Action<string>>' but required in type 'AnyAction'.ts(2345)

I have tried searching around for a solution to this issue, though without success. Most suggestions say to try and add an any param to the generic for dispatch, so dispatch<any>(actions.fetchActivities()). While this works to stop the typing error, only the first action in the thunk will get dispatched, while everything within the try and catch statements are ignored.

Does anyone have any suggestions as to how to fix this issue?

like image 729
Mike Hawkins Avatar asked Feb 04 '20 01:02

Mike Hawkins


People also ask

Is type 'type' assignable to parameter of type 'anyaction' in ts2345?

TS2345: Argument of type 'AsyncThunkAction<TicketEntity [], void, {}>' is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in type 'AsyncThunkAction<TicketEntity [], void, {}>' but required in type 'AnyAction'.

What is thunkaction in typescript?

The definition of ThunkAction is complicated and it is hard to understand how it is using TypeScript's type aliases. In this section, the ThunkAction definition will be broken down into a simpler form by referring to the ( type NameResolver = () => string) definition.

How to solve the “argument of type “unknown” is not assignable to parameter of type?

To solve the "Argument of type 'unknown' is not assignable to parameter of type" TypeScript error, use a type assertion or a type guard to verify that the argument is of the expected type. The error is caused when a value of type unknown is passed to a function.

Why type property 'type' is missing in asyncthunkaction type?

Property 'type' is missing in type 'AsyncThunkAction<TicketEntity [], void, {}>' but required in type 'AnyAction'. Most likely you have two conflicting versions of redux somewhere in your node_modules. 4.0.x and 4.1.x are not compatible on a type level (and generally you should only have one redux version).


1 Answers

These are two distinct problems. Typings of any way will never influence runtime behaviour.

Firstly: So your thunk not firing any other action can only have one meaning: The other calls to dispatch are most likely never reached. Are you sure your call to fetch ever terminates? It might just wait forever for an answer or something like that.

Secondly, your typing problem: The normal Dispatch type just does not support thunks per default. Your solution with Dispatch<any> is okay. Otherwise, you could cast your dispatch function to ThunkDispatch from the redux-thunk package.

Edit: Sorry, this just looked exactly like the RTK error, but it is something different. (Original, possibly wrong answer)

This is a known bug in RTK with a currently open PR that would fix it - I didn't get around to finalize it since I'm sick at the moment, but you can use the CI build until we put out a new version. You can install it via

yarn add https://pkg.csb.dev/reduxjs/redux-toolkit/commit/933aa4a1/@reduxjs/toolkit or npm i https://pkg.csb.dev/reduxjs/redux-toolkit/commit/933aa4a1/@reduxjs/toolkit

like image 149
phry Avatar answered Oct 22 '22 23:10

phry