Skip to content
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

Store type mismatch when using redux-thunk #207

Closed
suzu2469 opened this issue May 12, 2020 · 23 comments · Fixed by #295
Closed

Store type mismatch when using redux-thunk #207

suzu2469 opened this issue May 12, 2020 · 23 comments · Fixed by #295

Comments

@suzu2469
Copy link

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.

@kirill-konshin
Copy link
Owner

Can you make a repo to reproduce this?

@suzu2469
Copy link
Author

Of course, I made it here (I rewrote it from the above code to match v6.0.0)

https://codesandbox.io/s/musing-brown-16nd2

@kirill-konshin
Copy link
Owner

Thank you, I will look into it.

@Michalphs
Copy link

@suzu2469 Did you find solution? I have the same problem...

@kirill-konshin
Copy link
Owner

I haven't had time to look into this particular issue, sorry. It's in the backlog, I will fix it as soon as I can. In the mean time you guys are most welcome to investigate it too.

@Hyokune
Copy link

Hyokune commented Jun 2, 2020

I seem to be having this issue (without redux-toolkit) as well but for the time being whenever I use the dispatch, I make sure to cast it to a ThunkDispatch so the type error disappears.
Can't say for sure if this is a good workaround.

(ctx.store.dispatch as ThunkDispatch<IApplicationState, void, AnyAction>)(action(value))

@eyalch
Copy link

eyalch commented Jun 16, 2020

This part looks relevant:

export declare type MakeStore<S = any, A extends Action = AnyAction> = (context: Context) => Store<S, A>;

From my understanding, the problem is that MakeStore is a function which returns a Store instead of something that extends Store, which is exactly the case with Redux Toolkit's configureStore which returns an EnhancedStore:

export function configureStore<
  S = any,
  A extends Action = AnyAction,
  M extends Middlewares<S> = [ThunkMiddlewareFor<S>]
>(options: ConfigureStoreOptions<S, A, M>): EnhancedStore<S, A, M>
export interface EnhancedStore<
  S = any,
  A extends Action = AnyAction,
  M extends Middlewares<S> = Middlewares<S>
> extends Store<S, A>

(source)

I tried to play with the TypeScript declarations but wasn't able to get it to work.

@kirill-konshin What do you think?

@kirill-konshin
Copy link
Owner

@eyalch makes sense to me. Feel free to send me a PR.

eyalch pushed a commit to eyalch/next-redux-wrapper that referenced this issue Jun 21, 2020
support stores that `extends Store` and thus providing typing for something like Redux Toolkit's `EnhancedStore`

BREAKING CHANGE: change some of the types' signatures
Fixes kirill-konshin#207
@nikhilraj1997
Copy link

@kirill-konshin could you please approve the PR so that this issue is closed. This behavior is unwanted in case of redux-thunk and it has been breaking applications for a some time

@kirill-konshin
Copy link
Owner

@nikhilraj1997 there are open questions in the PR, if you can help to resolve them — you're most welcome.

@nikhilraj1997
Copy link

@kirill-konshin I really appreciate your work on next-redux-wrapper and the fact that you are welcoming help from others but I don't believe I am qualified enough to resolve the issues. If I could I would have. I apologize for pushing you to close issues. It would be an honor to work on an open source project one day

@kirill-konshin
Copy link
Owner

kirill-konshin commented Jul 25, 2020

@nikhilraj1997 well go ahead and give it a shot ))). That’s what open source is all about ) I simply don’t have enough time at the moment and the proposed change tends to be a major release, which should not happen for a change like what’s proposed. So if anyone can step up and make it work within a minor release - I’d gladly accept the contribution. As of for now, either I’ll make a major release or find time to investigate...

@Kupstas
Copy link

Kupstas commented Aug 17, 2020

Any news?

@kirill-konshin
Copy link
Owner

I am still not having enough time to fix this at the moment. If anyone can attempt to fix it w/o publishing a major release — you are most welcome...

@adonig
Copy link

adonig commented Oct 16, 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 {};
};

@kirill-konshin
Copy link
Owner

@adonig you're right, that's the correct way, according to docs: https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type

import { configureStore } from '@reduxjs/toolkit'
import { useDispatch } from 'react-redux'
import rootReducer from './rootReducer'

const store = configureStore({
  reducer: rootReducer
})

export type AppDispatch = typeof store.dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>() // Export a hook that can be reused to resolve types

Looks like even Redux Thunk itself does not have a cleaner way to tell store that it has modified the dispatch signature.

Closing.

@kirill-konshin
Copy link
Owner

kirill-konshin commented Oct 29, 2020

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

kirill-konshin added a commit that referenced this issue Oct 29, 2020
@kirill-konshin
Copy link
Owner

Use #295 to track progress.

@kirill-konshin
Copy link
Owner

https://github.com/kirill-konshin/next-redux-wrapper/releases/tag/7.0.0-rc.1

@Kupstas
Copy link

Kupstas commented Dec 12, 2020

It's an awesome update but I still have some questions. I've already updated to rc1 and changed code like in example but dispatch in getInitialProps or in getServerSideProps waits AnyAction instead of ThunkAction. I'm using pure redux-thunk + redux-act

@kirill-konshin
Copy link
Owner

@Kupstas weird. Take a look at this demo here: https://github.com/kirill-konshin/next-redux-wrapper/tree/types-enchancements/packages/demo-dynamicAnyAction was not used.

@Kupstas
Copy link

Kupstas commented Dec 15, 2020

@kirill-konshin I removed type of makeStore method and it worked. Really thanks, you did a great job!

kirill-konshin added a commit that referenced this issue Apr 17, 2021
* Fix #280
* Fix #207
* Minor fixes
* Updated API
* Updated versions
* Update README.md
* Fixed demos & tests
* Github Action
* Make demo-dynamic private
* Fix #326
* Fix hot reloading issues by removing the store from window (#324)

Co-authored-by: Melanie Seltzer <melleh11@gmail.com>
Co-authored-by: Daniel Ferenc Balogh <danielferencortel@gmail.com>
@Ayman-Zein
Copy link

I use redux-thunk not redux-toolkit or typescript, and I have the same problem.
is any solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants