【问题标题】:combineReducers return type is inferred to have type anycombineReducers 返回类型被推断为任何类型
【发布时间】:2021-03-07 14:57:01
【问题描述】:

我在 JSDocs 上使用 typescript,我在输入应用程序的根状态时遇到了麻烦。 在关于如何将它与 typescript 一起使用的 redux 文档之后,我尝试使用我的 root reducer 的返回类型作为 root state state,但它推断为 any。我不确定这是否只是我的 redux 版本的错误或什么。这是我的代码 sn-p:

import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';

const homeReducer = (state: {cool: boolean}) => state

const reducerMap = {
  router: routerReducer,
  home: homeReducer,
};

/** @typedef {ReturnType<typeof rootReducer>} RootState*/

const rootReducer = combineReducers(reducerMap);
export default rootReducer;

我从中得到的类型是type RootState = any。 我的 redux 版本是 3.7.2

typescript playground

【问题讨论】:

  • 提醒我,rootReducer 是一个函数?请分享可重现的例子,我会尽力帮助 jsdocs
  • 刚刚添加了一个可重现的示例和一个指向 TS 操场的链接
  • 你需要使用JSDoc吗?任何你不能做的理由export type RootState = ReturnType&lt;typeof rootReducer&gt;
  • @LindaPaiste 因为我不想弄乱打字稿转译以及它带来的所有问题以及它引入的所有噪音。我更喜欢坚持使用 JSDoc

标签: typescript redux jsdoc


【解决方案1】:

所以,看起来 redux 版本 3.7.2 不包含正确的类型。希望它很容易升级到 4.0.5,现在我发布的类型是正确的。 如果有人有兴趣查看此 PR 上的升级更改: merge request

【讨论】:

    猜你喜欢
    • 2020-05-03
    • 1970-01-01
    • 2011-01-13
    • 2019-02-12
    • 2017-10-15
    • 2014-03-05
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多