我收到此 TypeScript 错误属性“hasError”不存在于类型“(状态:ErrorType | undefined,操作:ErrorActionType)=> ErrorType”。我认为它抱怨为减速器设置初始状态。
import { ErrorType, ErrorActionType } from '../actions';
import { ActionTypes } from '../types';
export const errorReducer = (
state: ErrorType = { hasError: false, errorMessage: '' },
action: ErrorActionType
) => {
switch (action.type) {
case ActionTypes.setError:
return action.payload;
default:
return state;
}
};
这是我的行动
import { Dispatch } from 'redux';
import { ActionTypes } from '../types';
export interface ErrorType {
hasError: boolean;
errorMessage: string;
}
export interface ErrorActionType {
type: ActionTypes.setError;
payload: ErrorType;
}
export const setError = (hasError: boolean, errorMessage: string) => {
return (dispatch: Dispatch) => {
dispatch<ErrorActionType>({
type: ActionTypes.setError,
payload: { hasError, errorMessage },
});
};
};
还有我的 reducer.index
import { combineReducers } from 'redux';
import { errorReducer } from './error';
import { loadingReducer } from './loading';
import { newsReducer } from './news';
import { schoolsReducer } from './schools';
import { StoreStateType } from '../types';
export const reducers = combineReducers<StoreStateType>({
isLoading: loadingReducer,
hasError: errorReducer.hasError,
errorMessage: errorReducer.errorMessage,
news: newsReducer,
schools: schoolsReducer,
});
这是应用程序状态类型
export interface StoreStateType {
isLoading: boolean;
hasError: boolean;
errorMessage: string;
news: NewsType[];
schools: SchoolType[];
}