redux中使用TS每次都要定义一遍类型?
Posted 万年打野易大师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redux中使用TS每次都要定义一遍类型?相关的知识,希望对你有一定的参考价值。
umi架构下:ts项目中redux定义module每次都要写一遍类型定义麻烦得很
typing文件夹下创建Redux.d.ts声明文件
import type { Effect, Subscription, ImmerReducer } from \'umi\';
declare module MyRedux {
// model
type Models<T> = {
namespace?: string;
state: T;
effects: Record<string, Effect>;
reducers: Record<string, ImmerReducer<T>>;
subscriptions?: Record<string, Subscription>;
};
}
// 导出成模块,再全局导出MyRedux,这样使用就不用再import type { Redux } from \'@/typings/redux\'; 了
export = MyRedux;
// 由于使用了import,此文件变成局部模块,其他地方使用只能import导入(import type { Redux } from \'@/typings/redux\';)才能使用
export as namespace MyRedux;
如何使用
models文件夹下创建staff.ts
type StaffSettingState = {
staff: Record<string, never>;
};
const StaffSettingsModel: MyRedux.Models<StaffSettingState> = {
namespace: \'staffSettingsModel\',
state: {
staff: {}
},
effects: {},
reducers: {},
};
export default StaffSettingsModel;
以上是关于redux中使用TS每次都要定义一遍类型?的主要内容,如果未能解决你的问题,请参考以下文章
有没有人在使用redux dev工具在TS中遇到这个错误? “'窗口'类型中不存在”属性'__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'。“
Redux - 每次使用新变量在reducer switch语句中定义新状态?
无法读取未定义的属性“类型”(react-router-redux)
已解决在react+ts中 atnd 用 upload 组件报错Failed to execute ‘readAsArrayBuffer,param 1 is notof type Blob(代码片段