【问题标题】:Problem with dispatch "Argument of type '(dispatch: Dispatch<T>) => Promise<void>' is not assignable to parameter of type 'T'."调度问题“'(dispatch: Dispatch<T>) => Promise<void>' 类型的参数不可分配给'T' 类型的参数。”
【发布时间】:2019-02-05 23:34:25
【问题描述】:

我正在开发新的应用程序,但遇到了错误。当我尝试调度操作时,我收到了这样的错误:

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

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

  • “反应”:“^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”,
  • “打字稿”:“^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());
      }
  };

【问题讨论】:

    标签: reactjs typescript react-redux frontend web-frontend


    【解决方案1】:

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

    原因:这是来自“@types/react”的打字稿抱怨。

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

    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
    

    【讨论】:

    • 很好,它解决了我的问题,但仍然......你知道我是否可以分配除“任何”以外的任何类型吗?我对打字稿很陌生,但一旦我明白了,我应该始终避免使用“any”类型。还是谢谢!
    【解决方案2】:

    这就是我如何将调度返回到我的 redux 打字稿

        return (dispatch: any) => {
          getPosts()
          .then(res => 
            dispatch({ type: DispatchTypes.FETCH_POSTS, notifications: res.data })
          )
          .catch(err => 
            dispatch({ type: "ERROR",msg: "Unable to fetch data" })
          );
        };
    

    也许放任何可以解决错误?

    dispatch: any
    

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 2019-09-05
      • 2020-08-19
      • 1970-01-01
      • 2021-08-05
      • 2019-01-08
      • 2018-05-08
      • 2019-03-04
      • 2019-07-27
      相关资源
      最近更新 更多