reactjs - React Redux Connect DefaultRootState 类型

标签 reactjs typescript redux react-redux

我正在升级到最新版本的 typescript/react/redux,但在 react redux 的连接函数中遇到编译器错误:

我使用 react redux 文档提供的以下内容输入我的 rootReducer :

export type RootState = ReturnType<typeof rootReducer>

typescript 推断出的 RootState 类型是:
const rootReducer: Reducer<CombinedState<{
    authedUser: User | null;
    loading: LoadingState;
    errors: ErrorState;
    ui: UiState;
    entities: EntitiesState;
    shoppingCart: ShoppingCart;
    router: RouterState<...>;
}>, AnyAction>

连接功能是这样的:
export default withRouter(
connect<MyStateProps, MyDispatchProps, MyOwnProps>(
    ({ authedUser, entities, router }: StoreState): MyStateProps => ({
        authedUser,
        currentCinema: getCurrentCinema(entities.cinemas),
        router
    }),
    (dispatch) => ({ dispatch })
)(
    ({ authedUser, currentCinema, router, history }: Props) => {
        ...

错误是:
  /Users/cyprien/projets/vad2/src/components/App/App.tsx
  TypeScript error in /Users/cyprien/projets/vad2/src/components/App/App.tsx(69,9):
  No overload matches this call.

  The last overload gave the following error.
  Argument of type '({ authedUser, entities, router }: CombinedState<{ authedUser: User | null; loading: LoadingState; errors: ErrorState; ui: UiState; entities: EntitiesState; shoppingCart: ShoppingCart; router: RouterState<...>; }>) => MyStateProps' is not assignable to parameter of type 'MapStateToPropsParam<MyStateProps, MyOwnProps, DefaultRootState>'.

  Type '({ authedUser, entities, router }: CombinedState<{ authedUser: User | null; loading: LoadingState; errors: ErrorState; ui: UiState; entities: EntitiesState; shoppingCart: ShoppingCart; router: RouterState<...>; }>) => MyStateProps' is not assignable to type 'MapStateToPropsFactory<MyStateProps, MyOwnProps, DefaultRootState>'.

    Types of parameters '__0' and 'initialState' are incompatible.
      Type 'DefaultRootState' is not assignable to type 'CombinedState<{ authedUser: User | null; loading: LoadingState; errors: ErrorState; ui: UiState; entities: EntitiesState; shoppingCart: ShoppingCart; router: RouterState<...>; }>'.

        Type 'DefaultRootState' is missing the following properties from type '{ authedUser: User | null; loading: LoadingState; errors: ErrorState; ui: UiState; entities: EntitiesState; shoppingCart: ShoppingCart; router: RouterState<...>; }': authedUser, loading, errors, ui, and 3 more.  TS2769

67 | export default withRouter(
68 |     connect<MyStateProps, MyDispatchProps, MyOwnProps>(
69 |         ({ authedUser, entities, router }: StoreState): MyStateProps => ({
   |         ^
70 |             authedUser,
71 |             currentCinema: getCurrentCinema(entities.cinemas),
72 |             router

最佳答案

我找到了这个代码示例:

connect<
  ReturnType<typeof mapStateToProps>,
  typeof dispatchProps, // use "undefined" if NOT using dispatchProps
  Diff<BaseProps, InjectedProps>,
  RootState
>(
mapStateToProps,
dispatchProps
)(Hoc);
connect的最后一个模板参数是RootState ,根状态的类型。所以解决方案是正确参数化连接。
早期的评论留给考古学家:
这要成为评论。我把它放在这里只是因为格式:
我在 index.d.ts 中找到了这个:
/**
 * This interface can be augmented by users to add default types for the root state when
 * using `react-redux`.
 * Use module augmentation to append your own type definition in a your_custom_type.d.ts file.
 * https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
 */
// tslint:disable-next-line:no-empty-interface
export interface DefaultRootState {}

关于reactjs - React Redux Connect DefaultRootState 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61355084/

相关文章:

javascript - 使用子组件中的函数更改父状态

javascript - 如何使用 redux-saga-test-plan 测试选择器功能

javascript - React Native : Redux-persist, 操作结果未持久化

javascript - 返回数组中的项目时,如何一次渲染一个项目?

reactjs - TS2532 - 对象可能未定义 - useRef 和 react-three-fiber

reactjs - 如何在reactjs中的另一个应用程序中创建可重用的组件?

typescript - 从泛型类型引用推断的文字类型

reactjs - 创建一个返回 react 组件类的 typescript 函数

arrays - TypeScript 使用推送将对象添加到数组

javascript - 如何在浏览器中加载 NPM 模块?