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

TypeScript: compare two enums by string

I have 2 enums:

enum Insurer {
  PREMERA = 'premera_blue_cross',
  UHC = 'united_health_care'
}

enum ProductSource {
  PremeraBlueCross = 'premera_blue_cross',
  UnitedHealthCare = 'united_health_care'
}

I try check if array of Insurer includes ProductSource:

const insurerArr: Insurer[] = [Insurer.PREMERA, Insurer.UHC]
insurerArr.includes(ProductSource.PremeraBlueCross)

But got an error from the TS compiler:

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

Argument of type 'ProductSource' is not assignable to parameter of type 'Insurer'.

There is a way to compare without do a casting to string and then to the other enum?

>Solution :

You may want to consider to switch to type instead of enum:

type Insurer = 'premera_blue_cross' | 'united_health_care';
type ProductSource = 'premera_blue_cross' | 'united_health_care';
const insurerArr: Insurer[] = ['premera_blue_cross', 'united_health_care'];
insurerArr.includes('premera_blue_cross');
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