使用打字稿反应 redux combinereducer 时出错

Posted

技术标签:

【中文标题】使用打字稿反应 redux combinereducer 时出错【英文标题】:error at react redux combinereducer with typescript 【发布时间】:2021-12-07 07:39:47 【问题描述】:

我正在尝试通过制作待办事项列表来学习 redux

但我遇到了一个错误,我无法解决这个问题

我的类型

  export interface TodoType 
  text: string;
  id: number;

export interface TodoState 
  todoList: TodoType[];


interface ADD_TODO 
  type: "ADD_TODO";
  payload: TodoType;

interface REMOVE_TODO 
  type: "REMOVE_TODO";
  payload: TodoType;

export interface AppState 
  todo: TodoState;

export type TodoAction = ADD_TODO | REMOVE_TODO;

TodoReducer.ts ;

import  TodoState, TodoAction  from "global";

const INITIAL_STATE: TodoState = 
  todoList: [],
;

export const TodoReducer = (state = INITIAL_STATE, action: TodoAction) => 
  console.log(state, action);
;

index.ts

import  TodoState  from "global";
import  combineReducers  from "redux";
import  TodoReducer  from "./reducers/TodoReducer";

export interface AppState 
  todo: TodoState;


const rootReducer = combineReducers<AppState>(
  todo: TodoReducer,
);
export default rootReducer;

我在 todo 中遇到错误:part

我的文件的index.ts

import ReactDOM from "react-dom";
import  Provider  from "react-redux";
import  applyMiddleware, createStore  from "redux";
import thunk from "redux-thunk";

import App from "./App";
import rootReducer from "./store";

const store = createStore(rootReducer, applyMiddleware(thunk));

ReactDOM.render(
  <Provider store=store>
    <App />
  </Provider>,

  document.getElementById("root")
);

如果你能帮我解决这个问题,我会很高兴

【问题讨论】:

【参考方案1】:

您的TodoReducer 应始终返回TodoState 类型。

目前,您只在减速器中记录 stateaction。填满它应该可以正常工作。

【讨论】:

【参考方案2】:

虽然它不能直接回答您的问题,但您确实应该改用我们的官方 Redux Toolkit 包,这是编写 Redux 逻辑的标准方法。它将简化您的代码,让您消除所有这些额外的操作和类型定义,并且与 TS 配合得很好:

https://redux.js.org/usage/usage-with-typescript

例子:

import  createSlice, PayloadAction  from '@reduxjs/toolkit'
import type  RootState  from '../../app/store'

// Define a type for the slice state
interface CounterState 
  value: number


// Define the initial state using that type
const initialState: CounterState = 
  value: 0


export const counterSlice = createSlice(
  name: 'counter',
  // `createSlice` will infer the state type from the `initialState` argument
  initialState,
  reducers: 
    increment: state => 
      state.value += 1
    ,
    decrement: state => 
      state.value -= 1
    ,
    // Use the PayloadAction type to declare the contents of `action.payload`
    incrementByAmount: (state, action: PayloadAction<number>) => 
      state.value += action.payload
    
  
)

export const  increment, decrement, incrementByAmount  = counterSlice.actions

// Other code such as selectors can use the imported `RootState` type
export const selectCount = (state: RootState) => state.counter.value

export default counterSlice.reducer

【讨论】:

以上是关于使用打字稿反应 redux combinereducer 时出错的主要内容,如果未能解决你的问题,请参考以下文章

反应路由器+打字稿类型错误

如何检查泛型 K 是不是属于泛型 M |打字稿通用 |还原

如何在打字稿中使用中间件键入 redux thunk

将 react-redux useSelector 与打字稿一起使用

使用 react-redux 连接功能时渲染组件上的打字稿错误

promise + redux + redux-thunk:打字稿错误“不可分配给类型'Promise<any>”