【问题标题】:How can I use a TypeScript enums values as type?如何使用 TypeScript 枚举值作为类型?
【发布时间】:2022-01-15 12:00:29
【问题描述】:

如何在ISupportedTemplateType 中使用打字稿枚举值作为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;
}

【问题讨论】:

    标签: typescript types enums enumeration


    【解决方案1】:

    你只是......使用它:

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

    然后这工作正常:

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

    Playground

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 2019-08-04
      相关资源
      最近更新 更多