Reactjs 类型为'的参数;(调度:调度)=>;无效';不可分配给类型为';任何行动';

Reactjs 类型为'的参数;(调度:调度)=>;无效';不可分配给类型为';任何行动';,reactjs,redux,async-await,react-redux,redux-thunk,Reactjs,Redux,Async Await,React Redux,Redux Thunk,错误本身: (alias) deleteCategory(id: number): (dispatch: Dispatch<AnyAction>) => void import deleteCategory Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in ty

错误本身:

(alias) deleteCategory(id: number): (dispatch: Dispatch<AnyAction>) => void
import deleteCategory
Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'.
  Property 'type' is missing in type '(dispatch: Dispatch) => void' but required in type 'AnyAction'.ts(2345)
删除类别的实施:

export const deleteCategory = (id: number) => (dispatch: Dispatch) => {
  deleteCategoryAPI(id)
    .then(() => {
      dispatch({
        type: DELETE_CATEGORY,
        category: id,
      });
    })
    .catch((error) => console.error(error));
};
deleteCategoryAPI的实现:

export async function addCategoryAPI(category: CategoryType) {
  return fetch(`${API_URL}/categories`, {
    method: "POST",
    body: JSON.stringify(category),
    headers: { "Content-Type": "application/json; charset=UTF-8" },
  }).then((response) => response.json());
}
我在这里使用了有问题的代码:

const categoryList = getCategoryList(categoryState, dispatch);
...
return (
    <List
      dataSource={categoryList}
      renderItem={({ name, onDelete }) => (
        <List.Item>
          <CategoryItem name={name} onDelete={onDelete} />
        </List.Item>
      )}
    />
  );
有什么问题吗?为此花了几个小时。
有趣的是,当我调用
dispatch(deleteCography(category.\u id))
功能组件中的其他地方时,例如,它可以正常工作。。多谢各位

> P>我可以首先考虑几种不同的方式,你可以考虑这取决于你想投入多少努力,对你想要的香草实现的真实性,以及你想要的准确度。
  • 最简单的实现和最真实的API(但键入错误):

  • 此项目符号特定于Redux Toolkit,但可以轻松应用于第三方库(例如Redux)的大多数类型脚本键入问题。在使用
    redux工具包
    的情况下,可以创建对
    configureStore
    createSlice
    的引用,并根据选择操作打字。在下面的代码中,我将createSlice函数包装在
    createSlicePlus
    中,在幕后添加了一系列方便的逻辑,并增强了参数和返回类型

    // convenience `request` wrapper around store `dispatch` that abstracts the rest
    const promise = counter.request.decrementThunk(500)
    // even the RTK thunk promise is typed appropriately
    promise.abort()
    
  • 可以找到第三个示例的代码

    function CategoryItem({ name, onDelete }: categoryItemPropsType) {
    
    export type categoryItemPropsType = {
      name: string;
      onDelete: () => void;
    };
    
    // create your own dispatch function reference with custom typings
    export const dispatchStore = store.dispatch as typeof store.dispatch | Dispatch<any>
    
    // use custom typed dispatch (unfortunately, you can pass it almost `any`thing)
    dispatchStore(myThunk('id123'))
    
    // optional
    export const dispatchStore = store.dispatch
    
    // type each dispatch as any (not good if you need to use 'abort', etc)
    dispatchStore(myThunk('id123') as any)
    
    // convenience `request` wrapper around store `dispatch` that abstracts the rest
    const promise = counter.request.decrementThunk(500)
    // even the RTK thunk promise is typed appropriately
    promise.abort()