Confirm that a specific value exists within an enumerated set

I am currently using Angular 13.3.9 and typescript 4.6.4.
My main objective is to determine if a value is referencing an enum.

Below is the code snippet:

export enum HttpFunctionalErrorCodes {
  ACCOUNT_NOT_FOUND = 'ACCOUNT_NOT_FOUND',
  USER_ALREADY_EXISTS = 'USER_ALREADY_EXISTS',
  BAD_CREDENTIALS = 'BAD_CREDENTIALS'
}

The export enum is part of an npm library that has already been compiled...

import { HttpFunctionalErrorCodes } from ...;

  computeError(error: any): void {
    console.log(error.code) // BAD_CREDENTIALS
    console.log(HttpFunctionalErrorCodes) // undefined
    if (!_.isNil(error.code) && error.code in HttpFunctionalErrorCodes) {
   // TypeError: Cannot use 'in' operator to search for 'BAD_CREDENTIALS' in undefined
      ...
    }
  }

In this code, I capture an error passed into the computeError function.
Within this method, my aim is to ascertain whether the code belongs to HttpFunctionalErrorCodes, which is an enum.
However, I encounter the following error:

TypeError: Cannot use 'in' operator to search for 'BAD_CREDENTIALS' in undefined

After some investigation, I came across this article: , which states that with a typical enum, there should be no issue.

I also attempted the following:

error.code in Object.values(HttpFunctionalErrorCodes)

This resulted in the error:

TypeError: Cannot convert undefined or null to object

Interestingly, when testing on TSPlayground, it worked! typescriptPlayground

How can I confirm that my value is indeed within an enum? Why do I receive this error?

EDIT

Thanks to the assistance of nicholas-k and further research, I discovered some discussions regarding this issue:

  • `Cannot read properties of undefined` for enum after bundling with webpack
  • Angular use enums from typescript npm package, undefined error

While it worked on stackblitz, I encountered the same error when trying it on my own library:

TypeError: Cannot use 'in' operator to search for 'BAD_CREDENTIALS' in undefined

export class HttpFunctionalErrorCodes {
  static ACCOUNT_NOT_FOUND = 'ACCOUNT_NOT_FOUND'
  static USER_ALREADY_EXISTS = 'USER_ALREADY_EXISTS'
  static BAD_CREDENTIALS = 'BAD_CREDENTIALS'
}

OR

export const HttpFunctionalErrorCodes = {
  ACCOUNT_NOT_FOUND: 'ACCOUNT_NOT_FOUND',
  USER_ALREADY_EXISTS: 'USER_ALREADY_EXISTS',
  BAD_CREDENTIALS: 'BAD_CREDENTIALS'
}

Answer №1

After much searching, I finally identified the issue!

The solution involved utilizing the following method:

export class HttpFunctionalErrorCodes {
  static ACCOUNT_NOT_FOUND = 'ACCOUNT_NOT_FOUND'
  static USER_ALREADY_EXISTS = 'USER_ALREADY_EXISTS'
  static BAD_CREDENTIALS = 'BAD_CREDENTIALS'
}

In addition, I took the necessary step of clearing (deleting) the angular cache located in the folder ".angular/cache/13.3.6/"

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

Angular2: Issue encountered while processing click event

When I click a button on my client application, it sends a request to the server I created using Express. The request handler in the server simply logs 'Delete from server' every time the button is clicked. I am encountering these errors when cl ...

How come my uploaded Excel Javascript add-on opens in an external browser instead of the task pane?

Note: It has come to my attention that I must save the taskpane.html file on my local drive before it opens in an external browser. This detail slipped my notice last week. I am currently developing a Javascript, or rather Typescript, API add-in for Excel ...

Node.js application encountered an issue when running npm start due to the absence of toposcache.json file

I am facing an issue with my node js application that uses the topos package. I have performed npm install and then npm start, but I keep getting an error message below. I'm not sure what I might be missing. Any help would be greatly appreciated. top ...

Can Angular Universal help pinpoint the location of a window reference error?

My Angular Universal project was running smoothly until I added a significant amount of code and included some external npm libraries like Quill. Now, I am encountering a Reference error related to the window object. It seems that every time I reference wi ...

Is there a method to incorporate a scroll event into the ng-multi-selectdropdown, a npm package?

Query: I need to incorporate a scroll event in the given html code that triggers when the div is scrolled. However, I am facing issues with implementing a scroll event that works when the elements are being scrolled. <ng-mult ...

Transferring Complex Data Structures from the Realtime Database to Firestore

I am currently in the process of migrating data from Firebase Realtime Database to Firestore, specifically dealing with nested data that I would like to organize into a collection. For example: "data" : { "-LYBzlXPoN0137KRLovk" : { "-LYC-HHqDFgL9Po ...

Strategies for avoiding unused style tags in React components

Expanding beyond React, I'm unsure if React itself is the culprit of this issue. In a React environment with TypeScript, I utilize CSS imports in component files to have specific stylesheets for each component. I assumed these styles would only be ad ...

What is causing the duplication of leaves when using this DFS implementation?

I created an algorithm to compare if two trees have the same leaves. Both trees display matching leaf numbers in the exact order, resulting in a true outcome. Below is the code that I formulated: function leafSimilar(root1: TreeNode | null, root2: TreeNo ...

Can you identify the TypeScript type for an array containing various Angular components?

In my application, I have a diverse range of components that I would like to organize into an array. There are no restrictions on what types of components can be included in this array, as long as they are Angular components. What is the correct way to de ...

We encountered a ReferenceError stating that 'dc' is not defined, despite having already imported d3, dc, and crossfilter in

In my current angular project, I have included the necessary imports in the component.ts file in the specific order of d3, crossfilter2, dc, and leaflet. Additionally, I have added the cdn for dc-leaflet.js in the index.html file. Despite these steps, wh ...

ng-select issue: list not refreshing

I am encountering an issue with the method below that updates the modules array. Even though the console displays the result correctly, the ng-select does not update the list accordingly. I attempted to use this.modules=[...elements], but it did not work ...

What is the procedure for obtaining the corner coordinates of a boundary box in Cesium, expressed in CRS

I am currently developing a project where I am utilizing Cesium to display WMS layers on a map in 2D. To enhance performance, I have opted to use the SingleTileImageryProvider to request only one tile at a time. However, I've encountered an issue whe ...

Tips for choosing a specific quantity and adjusting its value

Just starting out with Ionic 3 and looking for some help with the code. Can anyone assist me in understanding how to change the value of an item in a shopping cart and have the subtotal reflect that change? cart.ts private _values1 = [" 1 ", "2", " 3 "," ...

The Vue store array declaration triggers a TS error stating that it is not assignable to a parameter of type never

I'm puzzled as to why this error keeps showing up: Argument of type '{ id: string; }' is not assignable to parameter of type 'never'. ... appearing at const index = state.sections.findIndex((section) => section.id === id); T ...

What steps can I take to troubleshoot and resolve any errors that arise while using Strapi

I'm in the process of building a portfolio website using Gatsby and Strapi. Everything was going smoothly until I attempted to input a large amount of data into strapi, resulting in an error message stating "An error occurred" and causing the developm ...

Transitioning away from Bower in Workflow and Embracing npm

As the popularity of npm continues to rise, I find myself questioning the necessity of using bower in my workflow. I have come across numerous articles explaining why transitioning from bower to npm is beneficial, but a comprehensive guide on how to migra ...

Node.js has been giving me trouble as I try to install Inquirer

:***Hey Guys I'm Working on a TypeScript/JavaScript Calculator! ...

What is the best way to transform this Ajax query into an HTTP client request in Angular 8?

I have an ajax request that looks like this: var data = { REQUEST: 'GetFeatureInfo', SERVICE: 'WMS', VERSION: '1.1.1', LAYERS: layerName, STYLES: '', FORMAT: 'image/png', INFO ...

Sharing a Twitter thread using Twit library with Node.js

I have been using Node.js along with the npm Twit module to post tweets on Twitter, and while it works for a single tweet, I am facing issues when trying to post multiple tweets as a thread. When attempting to post a series of tweets together, they do not ...

Interfaces and Accessor Methods

Here is my code snippet: interface ICar { brand():string; brand(brand:string):void; } class Car implements ICar { private _brand: string; get brand():string { return this._brand; } set brand(brand:string) { this. ...