0

对打字稿来说很新——我已经根据 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>'.

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

4

1 回答 1

2

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

import { StateType } from "typesafe-actions";

declare global {
  export type SpaceState = StateType<
    ReturnType<typeof import("./root_reducer").rootReducer>
  >;
}
于 2021-05-31T15:17:50.730 回答