Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

    <span>{Label[specialist.label] }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading