Store type mismatch when using redux-thunk
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:10
- Comments:23 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
While the issue isn’t fixed you can use the following workaround:
P.S. Will be fixed in
v7.0
.