【问题标题】:Typescript: Custom interface type using enumTypescript:使用枚举的自定义接口类型
【发布时间】:2021-01-24 20:50:47
【问题描述】:

我正在使用我的 API 端点处理类别,因此我为它创建了这个 enum

export enum categories {
  DESIGN = 'design',
  ART = 'art',
  WRITING = 'writing',
  PHOTOGRAPHY = 'photography',
  MUSIC = 'music',
  DIGITAL = 'digital',
  MEDIA = 'media'
}

现在我想用它来为我的数据库中的category 属性设置一个接口。

interface

interface ProjectAttrs {
  user: string;
  title: string;
  description: string;
  category:   // <== I want it to be either of those values defined above
}

如何将类别接口类型指定为枚举中定义的任一值?

【问题讨论】:

  • 尝试category: categories,如果这对您不起作用,请尝试发布minimal reproducible example,显示问题所在以及您正在尝试执行的操作。

标签: javascript node.js typescript types enums


【解决方案1】:

只需像这样输入枚举名称categories

interface ProjectAttrs {
  user: string;
  title: string;
  description: string;
  category: categories;
}

这是一个有效的demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-24
    • 2011-09-11
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多