簡體   English   中英

Typescript:使用枚舉的自定義接口類型

[英]Typescript: Custom interface type using enum

我正在使用我的 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
}

如何將類別接口類型指定為枚舉中定義的任一值?

只需像這樣放置枚舉名稱categories

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

這是一個工作演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM