question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Store type mismatch when using redux-thunk

See original GitHub issue

I want to use thunk’s AsyncAction from getInitialProps, but the type doesn’t match.

// --- In store module file
const fetch = ReduxToolkit.createAsyncThunk('count/fetch', async () => {
    // ... some codes
})

// --- In page file
const Page: Next.NextPage = () => {
    const count = useSelector(state => state.count)
    return <div>{count}</div>
}

Page.getInitialProps = async ({ store }) => {
    // TS2345: Argument of type 'AsyncThunkAction<Count, undefined, AsyncThunkConfig<unknown>>' is not assignable to parameter of type 'AnyAction'.   Property 'type' is missing in type 'AsyncThunkAction<Count, undefined, AsyncThunkConfig<unknown>>' but required in type 'AnyAction'.
    await store.dispatch(fetch())
    return {}
}

So this is how I wanted to solve it, but I couldn’t.

// --- In index.d.ts
port { NextPageContext } from 'next'
import { EnhancedStore } from '@reduxjs/toolkit'
import { ThunkMiddleware } from 'redux-thunk'

declare module 'next/dist/next-server/lib/utils' {
    export interface NextPageContext<S = any, A extends Action = AnyAction> {
        store: EnhancedStore<S, A, [ThunkMiddleware<S, A>]>
    }
}

I’m using redux-toolkit, but I think maybe even if someone is using pure thunk they’ll have the same problem. Please let me know if you have any other solutions.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

11reactions
adonigcommented, Oct 19, 2020

While the issue isn’t fixed you can use the following workaround:

/* Export a dispatch type. */
export type AppThunkDispatch = ThunkDispatch<State, void, AnyAction>;
MyNextPage.getInitialProps = async ({ store }) => {
  /* Cast the store's dispatch function to the new type. */
  const dispatch = store.dispatch as AppThunkDispatch;
  await dispatch(someThunkAction());  // Now tslint doesn't complain anymore.
  return {};
};
10reactions
kirill-konshincommented, Oct 29, 2020

P.S. Will be fixed in v7.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running into Typescript state type mismatch error inside of ...
Running into Typescript state type mismatch error inside of Redux store file ; import { applyMiddleware, createStore, combineReducers } from ...
Read more >
How to Use Thunks with Redux Toolkit and TypeScript
Thunks are a way to manage side effects when working with Redux. ... the store's abilities and lets you write async logic that...
Read more >
Handling API request race conditions in React
There are multiple ways to load data into a Redux store. Generally, if you are using Redux-saga or Redux-observable, you are fine. For...
Read more >
import provider from 'react-redux' error - You.com - You.com
This error typically occurs when you have more than one children retrieved (for example, if you render a component that has several elements...
Read more >
[Solved]-Type mismatch in typescript and react-Reactjs
[Solved]-Type mismatch in typescript and react-Reactjs. Search. score:3. const Example = (props: { x?: number, fn: (x: number) => void}) => { if...
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