Karan Kumar
Karan Kumar

Reputation: 3186

Typescript: Custom interface type using enum

I am working with categories with my API endpoint and so I have created this enum for it :

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

And now I want to use this to set an interface for the category attribute in my database.

interface for that :

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

How do I specify the category interface type to be either of the values defined in the enum?

Upvotes: 0

Views: 174

Answers (1)

Asaf
Asaf

Reputation: 1564

Just put the enum name categories like this:

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

Here is a working demo

Upvotes: 1

Related Questions