How to compare two enums in TypeScript using their string values?

I am working with 2 enums:

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

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

I attempted to check if an array of Insurers includes a Product Source:

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

However, I encountered an error from the TypeScript compiler:

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

Is there a method to compare without needing to cast to string and then to the other enum?

Answer №1

It might be worth considering switching to the type instead of using enum:

type Provider = 'blue_shield' | 'aetna';
type ServiceSource = 'blue_shield' | 'aetna';
const providerList: Provider[] = ['blue_sheild', 'aetna'];
providerList.includes('blue_shield');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery to input a value that returns the [object Object]

While working with jQuery in asp.net, I encountered an issue where the value assigned to a hidden field (hfstockcode) is returning [object Object]. The console output shows v.fn.v.init[1]. How can I retrieve the correct value for the hidden field? $(docum ...

Jquery Triggers Failing to Work Following Ajax Request

I have worked on 2 PHP pages, called "booking.php" and "fetch_book_time.php". Within my booking.php (where the jquery trigger is) <?php include ("conn.php"); include ("functions.php"); ?> $(document).ready(function(){ $(".form-group"). ...

When emitting an event multiple times in Angular, an error may occur where properties of undefined are unable to be read, particularly in relation to the "

I am encountering an issue with my event binding on a button, specifically (click)="onStart()". The problem arises when the event this.numEmitter is emitted for the first time in setInterval, after which I receive the error message ERROR TypeError: Cannot ...

What steps should I take to address the ES6 promise chain issue within Express?

Currently, I am delving into Promise chaining and experimenting with some code involving express and mongoose in a node environment. Below is the snippet of code that I am working on: const register = (req, res, next) => { User.findOne(req.params.ema ...

How many files are being monitored by Grunt?

Recently, I received a new project using Angular + Node from a client and successfully set it up on my local machine. However, one major issue arose when running the grunt command - my CPU spiked to 100% causing my system to hang. Strangely, the same proje ...

Incorporating the values of JavaScript objects into a global variable

I'm currently developing a bank account program and facing a challenge in adding my direct debits (DDs) into the global variable. When I create new objects using my constructor function and add them into the bank account, only the last created DD is s ...

What exactly is the purpose of measuring "First Load JS" in the @next/bundle-analyzer tool?

The analysis generated by the NextJS bundle analyzer appears as follows: Page Size First Load JS ┌ λ / 12 ...

The dropdown menu stubbornly refuses to close even after multiple clicks on it in the mobile responsive user interface

Currently, I am tackling the challenge of creating a website navigation bar with a dropdown menu that adjusts to different screen sizes. However, I have encountered an obstacle where the dropdown menu fails to close when I click outside of it. To address t ...

Not enough test coverage with Jest

Here is the code snippet I am working with: <ReturnToLastSearch href={'/listings'} onClick={(e): void => { e.preventDefault(); router.back(); }} /> The function ReturnToLastSearch( ...

What is the best way to display JSON within a partial?

Within my Rails app, I have implemented a JavaScript graph. Whenever I click on a circle in the graph, it displays the name of the corresponding user. Now, my goal is to retrieve additional information from the related database table based on this user&apo ...

How can one access a div tag within a script using Jquery?

I am faced with a challenge related to accessing the div tag "warningMessage" within my HTML code. Below is the snippet that contains this div tag under scripts: <script id="notePopup" type="text/x-kendo-template"> <p class="notePopupMessage" ...

Top method for identifying discrepancies in 2 arrays of objects

I'm currently working with two arrays of objects and trying to find the most efficient way to compare them to identify any differences. https://i.sstatic.net/fIPnA.png I apologize for posting the image, but I felt it was necessary for better clarit ...

Using JSP in combination with JavaScript for creating dynamic templates

Implementing server-side HTML templating with JSP + JSTL allows for the generation of tables containing user data: <body> ... <table> <c:forEach items="${users}" var="user"> <tr> <td>${user ...

Ensuring Type Safety with Setter-Only Properties in TypeScript

I found it quite surprising that this piece of code compiles without any issues in TypeScript, but throws an error at runtime: class Y { set writeOnlyProp(value: number) { // perform some actions here } } const y = new Y() // runtime error ...

Getting a user's group name from Azure Active Directory in an Angular application - a step-by-step guide

I have connected to Azure directory using ng2-adal (https://github.com/mazhisai/ng2-adal-QuickStart) and successfully obtained a group id in the format: "groups":["4ba2649e-20d2-40f4-a406-2ed897686403","43e19f05-c077-4716-b001-0ffb0d75fff8"]. Is there a w ...

What is the simplest way to display an HTTP response in an alert window?

Struggling to display the JSON response using JavaScript's window.alert or alert. As a non-native JS coder, I apologize for my lack of experience. Here are a few attempts I've made based on online examples. My objective is to showcase the JSON r ...

Error message: "@ViewChild is not defined within the ngAfterViewInit lifecycle hook

I've encountered an issue with *ngIf and @ViewChild that I haven't been able to solve despite trying various recommended solutions from similar questions. Here is a snippet of my HTML file: <div id="main-container" class="page-layout blank ...

What is the best way to reset the state to null when the input field is blank?

After setting some inputs with a state of null, I noticed that when the end-user types something and then deletes it all, the input reverts back to an empty string. How can I adjust the state so that it returns to null? I seem to be encountering an issue ...

How can the background color of a DIV be altered based on the values of an array's minimum

Basically, I am fetching values from an array using AJAX. The background color will change from green to red based on the "aht_value". Currently, the colors are displaying correctly because I hardcoded the values in the calculation below. However, I want t ...

Press a single button to toggle between displaying and hiding the table

$(document).ready(function() { $("#t1").hide(); // hide table by default $('#sp1').on('click', function() { $("#t1").show(); }); $('#close').on('click', function() { $("#t1").hide(); }); }); <li ...