简体   繁体   English

TypeScript,反应测试 AnyAction 中缺少的类型

[英]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.

相关问题 类型“提供商” <AnyAction> &#39; 不见了 - Type 'Provider<AnyAction>' is missing React-TypeScript - 类型 &#39;(dispatch: Dispatch) 上不存在属性 &#39;then&#39;<AnyAction> ) =&gt; 承诺<void> &#39; - React-TypeScript - Property 'then' does not exist on type '(dispatch: Dispatch<AnyAction>) => Promise<void>' 类型“AsyncThunkAction”中缺少属性“类型” <device, number, { dispatch: dispatch<anyaction> ; }&gt;' 但在 'AnyAction' 类型中是必需的</device,> - Property 'type' is missing in type 'AsyncThunkAction<Device, number, { dispatch: Dispatch<AnyAction>; }>' but required in type 'AnyAction' 类型“AsyncThunkAction”中缺少属性“类型”<fetchuserresponse, void, {}> ' 但在 'AnyAction' 类型中是必需的</fetchuserresponse,> - Property 'type' is missing in type 'AsyncThunkAction<fetchUserResponse, void, {}>' but required in type 'AnyAction' React + Typescript:类型*中缺少属性* - React + Typescript: Property * is missing in type * 在实现 Redux-Persist 时使用 AnyAction 反应 Redux Typescript 错误 - React Redux Typescript error with AnyAction when implementing Redux-Persist 缺少函数的返回类型 - 在反应(打字稿)代码中 - Missing return type on function - in react (typescript) code React + Typescript:render prop属性*类型缺失 - React + Typescript: render prop Property * is missing in type 与Typescript反应:类型缺少属性映射 - React with Typescript: property map is missing for type Typescript 错误:TS2345:类型参数'(调度:调度)=&gt; Promise<void> ' 不可分配给“AnyAction”类型的参数</void> - Typescript error: TS2345: Argument of type '(dispatch: Dispatch) => Promise<void>' is not assignable to parameter of type 'AnyAction'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM