redux-mock-store icon indicating copy to clipboard operation
redux-mock-store copied to clipboard

Types for dispatch with thunk are incorrect

Open ChuckJonas opened this issue 7 years ago • 8 comments

If I load you async example into a project, i get the following error on return store.dispatch(fetchData())

Argument of type '(dispatch: any) => Promise<any>' is not assignable to parameter of type 'Action'.
  Property 'type' is missing in type '(dispatch: any) => Promise<any>'.

Using Redux 4.0

ChuckJonas avatar May 10 '18 20:05 ChuckJonas

Can confirm, with redux 4 types ain’t working.

flq avatar Jun 01 '18 18:06 flq

I can confirm it too.

martpie avatar Jun 07 '18 11:06 martpie

Since it doesn’t look lile this package is maintained we switched to some custom code - at the end we didn’t need full store semantics in our action tests, but just a working dispatcher and a way to collect dispatched actions

flq avatar Jun 07 '18 17:06 flq

This is probably a problem with the @types/redux-mock-store package rather than this one. https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/redux-mock-store

That package lists [email protected] as a dependency and since there were significant type changes between redux@3 and redux@4 new types will need to be submitted to DefinitelyTyped that are compatible with redux@4

chungnho avatar Jun 19 '18 19:06 chungnho

Just submitted this update which should allow you to add the ThunkDispatch signature when creating the store: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/26732

When that is merged and released you can do something like:

const middlewares: Array<Middleware> = [thunk]
type DispatchExts = ThunkDispatch<RootState, undefined, AnyAction>
const mockStoreCreator: MockStoreCreator<RootState, DispatchExts> = 
    createMockStore<RootState, DispatchExts>(middlewares)
const store: MockStoreEnhanced<RootState, DispatchExts> = mockStoreCreator({})

chungnho avatar Jun 21 '18 21:06 chungnho

@chungnho Thank you sir. Took me 5 hours to find this.

mpcen avatar Jul 07 '18 22:07 mpcen

👍 to put this in the docs

kgajowy avatar Sep 21 '18 18:09 kgajowy

I made a TypeScript fork of this library and resolved some open issues at @jedmao/redux-mock-store.

jednano avatar Jul 12 '19 06:07 jednano