English 中文(简体)
类型中,<{},任何,发送<Un knownAction>均不能被分配到“中世纪之光”的类型中。 定义红色沉积时
原标题:Type Middleware<{}, any, Dispatch<UnknownAction>> is not assignable to type Middleware<{}, any, Dispatch<AnyAction>> when define the redux logger

我界定了红色标识代码redux-logger”:“^3.0.6”,载于对store.ts。 类似:

import { configureStore } from  @reduxjs/toolkit ;
import rootReducer from  @/redux/reducer/combineReducer ;
import { createLogger } from  redux-logger ;
import thunk from  redux-thunk ;
import * as Redux from "redux";
import { Middleware } from  redux ;

const logger: Redux.Middleware = createLogger(); 
const initialState = {};

const store = configureStore({
    reducer: rootReducer,
    middleware: [logger as Middleware, thunk],
    devTools: process.env.NODE_ENV !==  production ,
    preloadedState: initialState
});

export default store;

然后使用<代码>pnpm 跑道对项目进行汇编。

src/redux/store/store.ts:8:7 - error TS2322: Type  Middleware<{}, any, Dispatch<UnknownAction>>  is not assignable to type  Middleware<{}, any, Dispatch<AnyAction>> .
  Type  (next: (action: unknown) => unknown) => (action: unknown) => unknown  is not assignable to type  (next: Dispatch<AnyAction>) => (action: any) => any .
    Types of parameters  next  and  next  are incompatible.
      Types of parameters  action  and  action  are incompatible.
        Type  unknown  is not assignable to type  AnyAction .

8 const logger: Redux.Middleware = createLogger();
        ~~~~~~


Found 1 error in src/redux/store/store.ts:8

 ELIFECYCLE  Command failed with exit code 2.

我失踪了吗? 我应该怎样确定这一问题?

问题回答

您需要明确提及<代码> 类型> 。 中东。

const logger: Redux.Middleware<{}, any, Dispatch<AnyAction>> = createLogger(); 

Or use getDefaultMiddleware:

import { configureStore } from  @reduxjs/toolkit ;
import { createLogger } from  redux-logger ;

const logger = createLogger();

const store = configureStore({
  reducer: () => {},
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger),
});

export default store;

一揽子方案:

"devDependencies": {
  "@types/redux-logger": "^3.0.13",
  "redux-logger": "^3.0.6",
  "typescript": "^5.2.2",
  "vite": "^5.1.1"
},
"dependencies": {
  "@reduxjs/toolkit": "^2.2.1"
}

https://stackblitz.com/edit/vitejs-vite-sch4bf?file=src%2Ftypescript.svg”rel=“nofollow noreferer”>stackblitz





相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签