目前我是打字机的新手,我正尝试将其与redux动作和reducer结合使用。我现在遇到错误,我想要最好的方法来重构代码。这是我的方法,我需要帮助。
动作
import Axios from "axios";
export const fetchTODO = term => async dispatch => {
dispatch({ type: "FETCH_TODO" });
try {
const response = await Axios.get(`/api/TODO/${term}`, {});
dispatch({
type: "FETCH_TODO_SUCCESS",
payload: response.data
});
} catch (error) {
dispatch({ type: "FETCH_TODO_FAILURE", error });
}
};
减速器
const initialState = {
payload: [],
isLoading: false,
error: {}
};
export default (state = initialState, { type, payload, error }) => {
switch (type) {
case "FETCH_TODO":
return { ...state, isLoading: true };
case "FETCH_TODO_SUCCESS":
return { ...state, payload, isLoading: false };
case "FETCH_TODO_FAILURE":
return { ...state, error: error, isLoading: false };
default:
return state;
}
};
打字稿代码
types.tc
export enum ActionTypes {
fetchTodos,
fetchTodosSuccess,
fetchTodosFailure
}
动作
import { ActionTypes } from "./types";
import Axios from "axios";
import { Dispatch } from "redux";
export interface Todo {
id: number;
title: string;
completed: boolean;
}
export interface FetchTodoAction {
type: ActionTypes;
payload?: Todo[];
error?: object;
}
export const fetchTransaction = (term: string) => async (
dispatch: Dispatch
) => {
dispatch({ type: ActionTypes.fetchTodos });
try {
const response = await Axios.get<Todo[]>(
`https://jsonplaceholder.typicode.com/todos/`
);
dispatch<FetchTodoAction>({
type: ActionTypes.fetchTodosSuccess,
payload: response.data
});
} catch (error) {
dispatch({ type: ActionTypes.fetchTodosFailure, error });
}
};
StateInterface对象
export interface StateInterface {
payload?: Todo[];
isLoading: boolean;
error?: object;
}
减速器
import { Todo, FetchTodoAction } from "./../actions/index";
import { ActionTypes } from "../actions/types";
import { StateInterface } from ".";
const initialState = {
payload: [],
isLoading: false,
error: {}
};
export const todosReducer = (
state: StateInterface = initialState,
{ type, payload, error }: FetchTodoAction
) => {
switch (type) {
case ActionTypes.fetchTodos:
return { ...state, isLoading: true };
case ActionTypes.fetchTodosSuccess:
return { ...state, payload, isLoading: false };
case ActionTypes.fetchTodosFailure:
return { ...state, error: error, isLoading: false };
default:
return state;
}
};
我在代码后收到此错误,任何人都可以告诉我最好的实现
(alias) const todosReducer: (state: StateInterface | undefined, { type, payload, error }: FetchTodoAction) => StateInterface
import todosReducer
No overload matches this call.
Overload 1 of 3, '(reducers: ReducersMapObject<StateInterface, any>): Reducer<CombinedState<StateInterface>, AnyAction>', gave the following error.
Argument of type '{ todos: (state: StateInterface | undefined, { type, payload, error }: FetchTodoAction) => StateInterface; }' is not assignable to parameter of type 'ReducersMapObject<StateInterface, any>'.
Object literal may only specify known properties, and 'todos' does not exist in type 'ReducersMapObject<StateInterface, any>'.
[[[我,我真的很希望得到一些支持,并在此先感谢您
undefined
)时,TypeScript会阻止您将参数作为选项(即undefined
)。您可以通过从initialState
推断类型来解决此问题。
例如const initialState: StateInterface { ... }
现在,您不再需要在化简器参数中设置状态的类型,因为TypeScript知道将始终定义该值。
例如export const todosReducer = (state = initialState, ...) => { ... }
因此,将减速器代码更改为以下代码:
import { Todo, FetchTodoAction } from "./../actions/index";
import { ActionTypes } from "../actions/types";
import { StateInterface } from ".";
// Define initial state type here
const initialState: StateInterface = {
payload: [],
isLoading: false,
error: {}
};
export const todosReducer = (
state = initialState, // state type is now inferred from initialState
{ type, payload, error }: FetchTodoAction
) => {
switch (type) {
case ActionTypes.fetchTodos:
return { ...state, isLoading: true };
case ActionTypes.fetchTodosSuccess:
return { ...state, payload, isLoading: false };
case ActionTypes.fetchTodosFailure:
return { ...state, error, isLoading: false };
default:
return state;
}
};
编辑:如果仍然无法解决问题,那么将
{ type, payload, error }: FetchTodoAction
更改为{ type, payload, error }: FetchTodoAction | undefined
应该可以解决问题。