reactjs - 调度问题“类型参数”(调度:调度) => 承诺' 不能分配给 'T' 类型的参数。”
问题描述
我正在开发新的应用程序,但遇到了错误。当我尝试调度操作时,出现如下错误:
'(dispatch: Dispatch) => Promise' 类型的参数不能分配给 'T' 类型的参数。类型 '(dispatch: Dispatch) => Promise' 中缺少属性 'type' 但在类型 'X' 中是必需的。
有没有人遇到过这个问题或可以帮助我?我正在使用包:
- “反应”:“^16.7.0”,
- “反应域”:“^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());
}
};
解决方案
我有一个类似的问题,刚刚解决。
原因:这是来自“@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
推荐阅读
- .net - 将 SqlDbType 转换为 .net 类型
- ubuntu - 卸载 CUDA 9.1 和安装 9.0 时遇到问题
- regex - sed 命令替换以空格和“ssid”而不是“scan_ssid”开头的字符串?
- ruby-on-rails - Rails:来自多个文件的同一模型的夹具
- shell - Gitlab CI:访问环境变量
- jenkins - 在 Jenkins Pipeline 中配置 Dockerhub & SVN 代码仓库
- algolia - 如何将原始 css 保留在链接到即时搜索 widgets.searchBox 的输入上
- java - 由于构造函数中的静态类,Mockito 无法使用 @InjectMocks 创建实例
- tensorflow - 模块“tensorflow.python.keras.datasets.fashion_mnist”没有属性“load_data”
- html - Bootstrap - 使列在断点上可见/隐藏不工作