我是打字脚本的新手,我一直在想该给dispatch指定哪种类型。现在我有了,但我怎么能把所有的动作都映射到它上呢?
这就是它的样子:
interface PropTypes {
title: string;
generalActions: any;
}
const mapDispatchToProps = (dispatch: any) => ({
generalActions: bindActionCreators(GeneralActions, dispatch)
});
我的类型文件:
export const ADD_PROJECT = "ADD_PROJECT";
export const DELETE_PROJECT = "DELETE_PROJECT";
export const CHANGE_TITLE = "CHANGE_TITLE";
export interface Project {
title: string;
subtitle: string;
}
export interface ChangeTitleAction {
type: typeof CHANGE_TITLE;
title: string;
}
// STATE
export interface ProjectsState {
projects: Project[];
}
export interface GeneralState {
title: string;
}
export type GeneralActionTypes = ChangeTitleAction;
export type ProjectActionTypes = AddProjectAction | DeleteProjectAction;
试试这个。
const dispatch: React.Dispatch<ProjectActionTypes | GeneralActionTypes>;
const mapDispatchToProps = (dispatch) => ({
generalActions: bindActionCreators(GeneralActions, dispatch)
});