我正在开发新的应用程序,但遇到错误。当我尝试调度动作时,出现了这样的错误:
类型'((dispatch:Dispatch)=> Promise'的参数不能分配给类型'T'的参数。 类型'((dispatch:Dispatch)=> Promise')中缺少属性'type',但类型'X'中则为必需。
有人遇到过这个问题或可以帮助我吗? 我正在使用软件包:
这是我的代码:
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());
}
};
答案 0 :(得分:0)
我有一个类似的问题,刚刚解决。
原因:这是来自“ @ 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
答案 1 :(得分:0)
这就是我如何将分派返回到我的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