权限码时有模块枚举和权限枚举共同组成的,比如:
enum PermModuleCodeEnum {
EnterpriseTenant = 'enterpriseTenant',
EnterpriseIncoming = 'enterpriseIncoming',
EnterpriseIncoming1 = 'enterpriseIncoming1',
}
type PremCode = 'Add' | 'Disable' | 'Edit'
function createPerCodes<T extends string>(union: T[], module: PermModuleCodeEnum): { [K in T]: `${PermModuleCodeEnum}:${K}` } {
return union.reduce((pre, cur) => ({ ...pre, [cur]: `${module}:${cur}` }), {} as { [K in T]: `${PermModuleCodeEnum}:${K}` });
}
const perCodes = createPerCodes<PremCode>(['Add', 'Disable', 'Edit'], PermModuleCodeEnum.EnterpriseTenant);
console.log('xxx-1', perCodes);
此时此时的 perCodes为 {
Add: "enterpriseTenant:Add" | "enterpriseIncoming:Add" | "enterpriseIncoming1:Add";
Disable: "enterpriseTenant:Disable" | "enterpriseIncoming:Disable" | "enterpriseIncoming1:Disable";
Edit: "enterpriseTenant:Edit" | "enterpriseIncoming" | "enterpriseIncoming1:Edit";
}
而不是
{
Add: "enterpriseTenant:Add";
Disable: "enterpriseTenant:Disable";
Edit: "enterpriseTenant:Edit";
}
怎样才能修复createPerCodes的类型定义呢