如何使用TypeScript枚举值作为类型



如何在ISupportedTemplateType中使用typescript枚举值作为templateTypeId的类型。值是1、2、3,我希望它是templateTypeId的类型

export enum ETemplateType {
'Cover Letter' = 1,
'Wire Instructions' = 2,
'Amendment' = 3,
}
export interface ISupportedTemplateType {
templateTypeId: number; // HOW CAN I USE ENUM HERE?
templateTypeName: keyof typeof ETemplateType;
}

你只是。。。使用它:

export interface ISupportedTemplateType {
templateTypeId: ETemplateType;
templateTypeName: keyof typeof ETemplateType;
}

然后这很好:

// works
const obj: ISupportedTemplateType = {
templateTypeId: ETemplateType['Cover Letter'],
templateTypeName: 'Cover Letter'
}

游乐场

最新更新