reactjs - 调度问题 "Argument of type '(调度 : Dispatch<T>) => Promise<void >' is not assignable to parameter of type ' T'.”

标签 reactjs typescript react-redux frontend web-frontend

我正在开发新应用程序,但遇到错误。当我尝试调度操作时,出现如下错误:

'(dispatch: Dispatch) => Promise' 类型的参数不可分配给类型 'T' 的参数。
类型 '(dispatch: Dispatch) => Promise' 中缺少属性 'type',但类型 'X' 中需要。

有没有人遇到过这个问题或可以帮助我?
我正在使用包:

  • “ react ”:“^16.7.0”,
  • "react-dom": "^16.7.0",
  • "react-redux": "^6.0.0",
  • "react-router-dom": "^4.3.1",
  • "redux": "^4.0.1",
  • "redux-thunk": "^2.3.0",
  • “ typescript ”:“^3.3.1”

  • 这是我的代码:
     Container:
    
     import { connect } from 'react-redux';
     import { Dispatch } from 'react';
     import { loginUser, Actions } from './actions';
     import Screen, { Props } from './Screen';
    
     const mapDispatchToProps = (dispatch: Dispatch<Actions >): Props => ({
       login: (userName: string, password: string) => { 
     dispatch(loginUser(userName, password)); }
     });
    
     export const ScreenContainer = connect(mapDispatchToProps)(Screen);
    
     Props:
    
     export interface Props {
      login: (userName: string, password: string) => void;
     }
    
    
    Actions:
    
    import { Dispatch } from 'redux';
    
    export type Actions =
      LoginRequest
      | LoginSuccess
      | LoginFailure;
    
      export const loginRequest = (): LoginRequest => ({
        type: types.LOGIN_REQUEST,
      });
    
      export const loginFailure = (): LoginFailure => ({
        type: types.LOGIN_REQUEST_FAILED,
      });
    
      export const loginSuccess = (): LoginSuccess=> ({
        type: types.LOGIN_REQUEST_SUCCESS,
      });
    
      export interface LoginRequest {
        type: types.LOGIN_REQUEST;
      }
    
      export interface LoginFailure {
        type: types.LOGIN_REQUEST_FAILED;
      }
    
     export interface LoginSuccess{
        type: types.LOGIN_REQUEST_SUCCESS;
      }
    
    export const loginUser = (email: string, password: string) => async 
     (dispatch: Dispatch<Actions>) => {
          dispatch(loginRequest());
          try {
            dispatch(loginSuccess());
          } catch (error) {
            dispatch(loginFailure());
          }
      };
    

    最佳答案

    我有一个类似的问题,刚刚解决。

    原因 :这是来自“@types/react”的 typescript 提示。

    解决方案 :
    添加代码如下:

    export const loginUser = (email: string, password: string) => async 
     (dispatch: Dispatch<Actions>) => {
         dispatch<any>(loginRequest());
         ....
      };
    
     add '<any>' into your complain place. it will passed TS compiler
    

    关于reactjs - 调度问题 "Argument of type '(调度 : Dispatch<T>) => Promise<void >' is not assignable to parameter of type ' T'.”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54544666/

    相关文章:

    javascript - 为 ReactJS 包装遗留小部件

    performance - React 组件隐藏还是重新创建?

    Angular - 在应用程序组件中获取路由数据

    javascript - 从数组中提取项目并使用新键添加到特定长度的新数组

    javascript - 空的缩小 React 应用程序,为什么它已经很大了?

    javascript - 如何使用原型(prototype)更新粗箭头函数?

    javascript - 如何通过代码清理使动态导入/需要依赖于变量?

    reactjs - 受控组件的内存使用问题

    reactjs - 将自定义 Prop 传递给 TypeScript 中的 Redux Form Field

    javascript - 在 Redux reducer 中取消将数据移至数组