[英]TypeScript, react testing missing type in AnyAction
I'm having this error:我有这个错误:
TS2345: Argument of type '(dispatch: Dispatch) => Promise<void>' is not assignable to parameter of
type 'AnyAction'. Property 'type' is missing in type '(dispatch: Dispatch) => Promise<void>' but
required in type 'AnyAction'. type' is declared here* :
*
The code of the declaration is: *
声明的代码是:
export interface Action<T = any> {
type: T
}
AnyAction extends Action. AnyAction 扩展了 Action。
This is my code on the test:这是我的测试代码:
import configureStore from 'redux-mock-store';
import reduxThunk from 'redux-thunk';
// some code, then in the test I have:
const mockStore = configureStore([reduxThunk]);
const store = mockStore({});
store.dispatch(signIn()); //here I have the error
The definition of signIn
is: signIn
的定义是:
export const signIn = () =>
async (dispatch: Dispatch): Promise<void> => {
dispatch({
type: SIGN_IN_REQUEST
});
};
Any hint or idea on how to fix it?关于如何修复它的任何提示或想法?
configureStore
allows us to pass arguments to express dispatch extensions so in order to make it work with redux-thunk
we have to specify ThunkDispatch
as argument as following: configureStore
允许我们传递 arguments 来表达调度扩展,所以为了让它与redux-thunk
一起工作,我们必须指定ThunkDispatch
作为参数,如下所示:
import { ThunkDispatch } from 'redux-thunk';
// your app state
interface AppState {}
// you can even express more params for `ThunkDispatch`
const mockStore = configureMockStore<AppState, ThunkDispatch<AppState, any, any>>([])
As tmho2005 said, I did that by reading his answer and another similar thread.正如 tmho2005 所说,我通过阅读他的回答和另一个类似的话题来做到这一点。 This is my code in case anyone wants it:
这是我的代码,以防有人需要它:
// only relevant code for the question
import { AnyAction } from 'redux';
import configureStore from 'redux-mock-store';
import reduxThunk, { ThunkDispatch } from 'redux-thunk';
import { AuthState } from '../../../types'; // that's the definition of my app state
type DispatchExts = ThunkDispatch<AuthState, void, AnyAction>;
// And the code for mys tests:
const mockStore = configureStore<AuthState, DispatchExts>([reduxThunk]);
const store = mockStore(defaultState);
await store.dispatch(signIn({
username: 'foo',
password: 'bar'
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.