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;

万年打野易大师
1.5k 声望1.1k 粉丝