Redux 的 combineReducers 和 TypeScript

Posted

技术标签:

【中文标题】Redux 的 combineReducers 和 TypeScript【英文标题】:Redux's combineReducers and TypeScript 【发布时间】:2018-12-10 09:48:12 【问题描述】:

我想在我的 React 应用程序中使用 react-intl-reduxredux-form,但在使用 combineReducers 时我无法正确执行操作。我已经尝试了很多东西,但仍然无法弄清楚。

react-intl-redux

import  combineReducers  from "redux";
import  intlReducer, IntlState  from "react-intl-redux";

export interface IAppState 
  intl: IntlState;


export default combineReducers<IAppState>(
  intl: intlReducer
);

[ts] 类型参数 ' intl: (state: IntlState, action: IntlAction) => 国际状态; ' 不可分配给“ReducersMapObject”类型的参数。财产类型“国际” 不兼容。 类型 '(state: IntlState, action: IntlAction) => IntlState' 不可分配给类型 'Reducer'。 参数 'state' 和 'state' 的类型不兼容。 输入'国际状态 | undefined' 不可分配给类型 'IntlState'。 类型“未定义”不可分配给类型“IntlState”。 (别名)函数 intlReducer(状态:IntlState,动作:IntlAction): IntlState 导入 intlReducer

redux-form

import  combineReducers  from "redux";
import  reducer as formReducer, FormState  from "redux-form";

export interface IAppState 
  form: FormState;


export default combineReducers<IAppState>(
  form: formReducer
);

[ts] 类型参数 ' form: FormReducer; ' 不可分配给 “ReducersMapObject”类型的参数。类型 属性“形式”不兼容。 类型“FormReducer”不可分配给类型“Reducer”。 参数 'state' 和 'state' 的类型不兼容。 输入'FormState | undefined' 不可分配给类型 'FormStateMap'。 类型“未定义”不可分配给类型“FormStateMap”。 (别名) const formReducer: FormReducer import formReducer

【问题讨论】:

CombineReducers with Typescript returns error "Argument of type is not assignable to parameter of type 'ReducersMapObject'"的可能重复 我特别期待看到react-intl-reduxredux-form 的打字稿实现。 npmjs.com/package/@types/react-intl-redux 和 npmjs.com/package/@types/redux-form ? 谢谢@falinsky,我有这一切。但我想看一个示例代码,因为我是 TypeScript 领域的新手,不知道如何实现这两个库。我对实现自己的减速器没有任何问题。 你解决问题了吗? 【参考方案1】:

尝试使用 FormStateMap 而不是 FormState。

【讨论】:

以上是关于Redux 的 combineReducers 和 TypeScript的主要内容,如果未能解决你的问题,请参考以下文章

combineReducers 破坏 Redux 状态并破坏多个 ImmutableJs 减速器

Redux - 理解 combineReducers

Redux 中的CombineReducer的函数详解

Redux 中 combineReducers实现原理

4 react 简书 引入 redux 的 combineReducers 对 redux 数据进行管理

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