如何在根模块中编写 rtk reducer?

huangapple go评论67阅读模式
英文:

How typing rtk reducer in root module?

问题

I understand that you want a translation of the code and relevant information without additional responses. Here's the translation:

我正在编写一个使用模块化架构的React应用程序每个模块都在单个接口之外导入

const authModule: IModule = {
  routes,
  reducers: {},
};

export type IModule = {
  routes?: IRoute[];
  reducers: IReducers;
};

然后所有模块都在根模块中收集

const modules: IModule[] = [
  authModule,
  userModule,
  weatherForecastModule,
  commonModule,
];

const routes = modules.reduce(
  (acc: IRoute[], module) => (!module.routes ? acc : [...acc, ...module.routes]),
  [],
);
const reducers = modules.reduce(
  (acc: IReducers, module) => ({ ...acc, ...module.reducers }),
  {},
);

const rootModule: IModule = {
  routes: routes,
  reducers: reducers,
};

export default rootModule;

然后我将所有的reducer添加到store中

const store = configureStore({
  reducer: {
    [baseApi.reducerPath]: baseApi.reducer,
    ...rootModule.reducers,
    userReducer,
    screenReducer,
  },
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(baseApi.middleware),
  devTools: true,
});

setupListeners(store.dispatch);

export default store;

在将reducers组装成一个模块的过程中它们的类型信息丢失了这就是为什么useAppSelector无法显示可能的reducers

const user = useAppSelector((state) => state.???);

附加信息

export type RootState = ReturnType<typeof store.getState>;
export type AppStore = typeof store;
export type AppDispatch = AppStore['dispatch'];

export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

Please let me know if you need any further assistance.

英文:

I am writing a react application using a modular architecture. Each module is imported outside a single interface.

const authModule: IModule = {
routes,
reducers: {},
};
export type IModule = {
routes?: IRoute[]
reducers:IReducers
};

Then all modules are collected into one in the root module:

const modules:IModule[] = [
authModule,
userModule,
weatherForecastModule,
commonModule,
];
const routs = modules.reduce(
(acc: IRoute[], module) =&gt; (!module.routes ? acc : [...acc, ...module.routes]),
[],
);
const reducers = modules.reduce(
(acc: IReducers, module) =&gt; ({ ...acc, ...module.reducers }),
{},
);
const rootModule: IModule = {
routes: routs,
reducers,
};
export default rootModule;

From which I add all reducers to the store:

const store = configureStore({
reducer: {
[baseApi.reducerPath]: baseApi.reducer,
...rootModule.reducers,
userReducer,
screenReducer,
},
middleware: (getDefaultMiddleware) =&gt; getDefaultMiddleware()
.concat(baseApi.middleware),
devTools: true,
});
setupListeners(store.dispatch);
export default store;

In the process of assembling reducers into one module, their typing is lost, which is why useAppSelector does not show possible reducers

const user = useAppSelector((state) =&gt; state.???);

Additional info:

export type RootState = ReturnType&lt;typeof store.getState&gt;;
export type AppStore = typeof store;
export type AppDispatch = AppStore[&#39;dispatch&#39;];
export const useAppSelector: TypedUseSelectorHook&lt;RootState&gt; = useSelector;

How fix it?

Simple example:
https://codesandbox.io/s/sleepy-wilbur-qj27hs?file=/src/modules/user/index.ts

答案1

得分: 1

无法绕过手动组装结果对象的类型。由于您将其转换为 IModule,TypeScript 无法以任何方式推断出这一点,因为它已经从中删除了所有“有趣”的类型信息。

英文:

You will not get around manually assembling the type for the resulting object.
There is no way for TypeScript to infer this in any way, considering that your cast to IModule already removes all "interesting" type information from them.

huangapple
  • 本文由 发表于 2023年7月13日 15:08:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定