【问题标题】:connected-router typescript declaring global state type连接路由器打字稿声明全局状态类型
【发布时间】:2021-05-31 15:10:35
【问题描述】:

typescript 的新手 - 我已经根据 redux root-reducer 的状态定义了我的全局状态。在我决定使用连接路由器之前,这一直很好用——它向根减速器添加了一些东西——现在我不确定如何声明全局状态。

例如...这是我的全局状态类型:

import { StateType } from "typesafe-actions";

declare global {
  export type SpaceState = StateType<
    typeof import("./root_reducer").rootReducer
  >;
}

如果我将鼠标悬停在 rootReducer 上,我可以看到我需要访问的属性:

const rootReducer: (history: History<unknown>) => Reducer<CombinedState<{
    router: RouterState<unknown>;
    ui: State;
    auth: State;
}>, AnyAction>

虽然在创建一个 redux 选择器来访问我的属性时,我得到了一个打字稿错误。首先是我的选择器,错误出现在.ui 属性下。

export const sidebarStatus = (state: SpaceState) => state.ui.sidebarShow;

这是我得到的错误:

Property 'ui' does not exist on type 'Reducer<CombinedState<{ router: RouterState<unknown>; ui: State; auth: State; }>, AnyAction>'.

我假设,因为我添加了连接路由器,我需要以不同的方式声明我的全局状态,但我不知道如何做到这一点。感谢您提供正确方向的任何帮助或指示。

【问题讨论】:

    标签: reactjs typescript redux connected-react-router


    【解决方案1】:

    啊,明白了!不得不使用 ReturnType

    import { StateType } from "typesafe-actions";
    
    declare global {
      export type SpaceState = StateType<
        ReturnType<typeof import("./root_reducer").rootReducer>
      >;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2017-08-16
      • 1970-01-01
      • 2022-01-06
      • 2017-08-12
      相关资源
      最近更新 更多