为什么? 'AsyncThunkAction<any, void, {}>' 类型的参数不可分配给 'AnyAction' 类型的参数

Why? Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction'

我收到这个错误,在 SO

中找不到相关答案

Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction'.

<MenuItem
    onClick={() => {
        dispatch(logout()) // <<--HERE
    }}
/>
// slices/tikexAPI.ts
export const logout = createAsyncThunk(`${namespace}/logout`, async () => {
    const { data } = await axios({
        method: 'post',
        url: 'logout',
        headers: { crossDomain: true },
    })
    return data
})

const tikexAPI = createSlice({
    name: 'authnUser',
    initialState,
    reducers: {
        setAuthnRes(state, action: PayloadAction<AuthnRes | null>) {
            state.resp = action.payload
        },
    },
    extraReducers: (builder) => {
        builder
            .addCase(logout.fulfilled, (state, { payload }) => {
                state.authnRes = null
            })
//store.ts

import { configureStore, createSelector } from '@reduxjs/toolkit'
import tikexAPI from './tikexModule/slices/tikexAPI'

export const store = configureStore({
    reducer: { tikexAPI: tikexAPI },
})

我只有两个 Redux 包,重新安装了它们,没有帮助。

// package.json

{
    "dependencies": {
        "@reduxjs/toolkit": "^1.8.1",
        "react-redux": "^8.0.1",

按照这个教程,对我有用,奇怪:https://www.youtube.com/watch?v=xtD4YMKWI7w

kukodajanos@Kukoda-MacBook-Pro-2 Team % yarn why redux
yarn why v1.22.18
[1/4]   Why do we have the module "redux"...?
[2/4]   Initialising dependency graph...
[3/4]   Finding dependency...
[4/4]   Calculating file sizes...
=> Found "redux@4.2.0"
info Reasons this module exists
   - "@reduxjs#toolkit" depends on it
   - Hoisted from "@reduxjs#toolkit#redux"
info Disk size without dependencies: "244KB"
info Disk size with unique dependencies: "1.06MB"
info Disk size with transitive dependencies: "1.11MB"
info Number of shared dependencies: 2
✨  Done in 0.29s.
kukodajanos@Kukoda-MacBook-Pro-2 Team % npm ls redux
tikex@0.1.0 /Users/kukodajanos/Workspace/Tikex/Portal/Team
└─┬ @reduxjs/toolkit@1.8.1
  └── redux@4.2.0 

您似乎没有使用正确类型的 useAppDispatch/useAppSelector 挂钩,至少这是该错误的最常见来源。请关注TypeScript QuickStart.


// app/store.ts

import { configureStore } from '@reduxjs/toolkit'
// ...

export const store = configureStore({
  reducer: {
    posts: postsReducer,
    comments: commentsReducer,
    users: usersReducer,
  },
})

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch

// app/hooks.ts

import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import type { RootState, AppDispatch } from './store'

// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch = () => useDispatch<AppDispatch>()
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector