简体   繁体   中英

NextJS TypeScript How to define Enum for strings

In my nextjs -app I have a component, which has a prop of the type string . Now I want to define an enum , so I tried to do this:

enum Label {
 dermatology = 'Dermatologi',
 psychology = 'Psykologi',
 rheumatology = 'Reumatologi',
 physiology = 'Fysiologi',

}

interface ISpecialist {
  label?: Label
}

export default function Specialist({ specialist }: { specialist: ISpecialist }) {
   return (
      <div>
        <span>{specialist.label === Label}
      </div>
   )
}

but this doesn't work - can someone help me out?

the prop label is as mentioned before of the type string and the values are for example 'psychology' or 'dermatology'

If you want to print the corresponding value of the enum, access it like the enum key.

    <span>{Label[specialist.label] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM