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

Creating a nested type using template literal syntax

Given a two-level nested type with specific properties: export type SomeNested = { someProp: { someChild: string someOtherChild: string } someOtherProp: { someMoreChildren: string whatever: string else: string } } I am looking ...

What is the syntax for creating a zip function in TypeScript?

If I am looking to create a zip function: function zip(arrays){ // assume more than 1 array is given and all arrays // share the same length const len = arrays[0].length; const toReturn = new Array(len); for (let i = 0; i < len; i+ ...

Navigate the nested route of a child page starting from the main Root component

I am facing an issue with enabling nesting routes on the BarcodeScannerComponent component. I have attempted the following method, but it does not seem to work. The main challenge lies in accessing the nested route within the root component, which is app.c ...

Error encountered with TypeScript template literals strings type

There's a new feature in the latest version of Typescript that allows the use of template literal strings as types, like this: type Hey = 'Hey'; type HeyThere = `${Hey} There`; It works perfectly in the Typescript playground with version 4 ...

When using npm global install on Windows 8.1, the packages are not automatically added to the PATH

After executing the command npm install -g <package>, the packages are installed in my user/AppData/Roaming/npm/npm_modules/ directory. However, since this sub-folder is not included in my PATH environment variable, attempting to run the package with ...

Is it possible to apply a style change to several components at once using a single toggle switch

I am looking to implement a day/night feature on my web app, triggered by a simple toggle click. While I can easily add this feature to a single component using the navigation menu, I am faced with the challenge of incorporating it into multiple component ...

Issue with router navigation not functioning within an output event in Angular 4

I've encountered a situation with my table component where it emits an event containing an id. `@Output() onClick: EventEmitter<any> = new EventEmitter<any>();` `onRowClick(id: any) { this.onClick.emit(id); }` `<a href="#" (click ...

What could be the reason for the image failing to generate?

I've encountered an issue with the following Dockerfile: FROM ubuntu:22.04 WORKDIR /root/app/frontend COPY . . ENV APP_NAME app.com ENV CONTAINER_PORT 80 ENV NODE_VERSION v20.10 EXPOSE $CONTAINER_PORT # utils RUN apt-get update \ &&a ...

Asynchronous and nested onSnapshot function in Firestore with await and async functionality

I'm facing an issue with the onSnapshot method. It seems to not await for the second onsnapshot call, resulting in an incorrect returned value. The users fetched in the second onsnapshot call are displayed later in the console log after the value has ...

The Nextjs next-auth implementation with URL '/api/auth/callback/cognito' encountered a 502 Bad Gateway error while in production

I'm facing a challenge while trying to deploy a nextjs app that utilizes 'next-auth' with AWS Cognito. Interestingly, the app runs smoothly when tested locally using either next dev or next start. However, upon deploying it on the producti ...

The error message "Property 'then' is not available on type 'void' within Ionic 2" is displayed

When retrieving data from the Google API within the function of the details.ts file, I have set up a service as shown below. However, I am encountering a Typescript error stating Property 'then' does not exist on type 'void'. this.type ...

Encountering build issues in my next.js application post updating to version 12.#.# and implementing Typescript

In my next.js application, I recently upgraded to version 10 and added TypeScript to the mix. Despite ironing out all issues during development, I encountered errors when running yarn next build due to my use of the keyword interface. ./src/components/ ...

Error message in Docker: "Command /bin/sh: 1: ng: not found; please make sure Angular

While attempting to create a docker image for my Angular app, I encountered an issue where it crashes on RUN ng build --prod and the error message displayed is: /bin/sh: 1: ng: not found The command '/bin/sh -c ng build --prod' returned a non-ze ...

Currently, I am utilizing version 6.10.0 of @mui/x-date-pickers. I am interested in learning how to customize the color and format of a specific component within the library

I'm currently using @mui/x-date-pickers version 6.10.0 and I need to customize the color and format for a specific section in the mobile view. How can I achieve this? I want to display the date as "May 31st" like shown in the image below. Is there a ...

Issues with Angular ng-bootstrap tabset component not functioning as expected

{ "name": "ModalWindow", "version": "1.0.0", "repository": { "type": "git", "url": "" }, "scripts": { "build": "webpack --mode production", "start": "webpack-dev-server --mode development --open" }, "license": "MIT", "depend ...

Is it possible to dynamically adjust the size of the CircleProgressComponent element in ng-circle-progress?

For my current Angular 11 project, I am facing the challenge of dynamically changing the size of the ng-circle-progress library's CircleProgressComponent element. After some research, I discovered that the element's size can be adjusted by apply ...

Moving the marker does not update the address

When the dragend event is triggered, the Getaddress function will be called in the following code: Getaddress(LastLat, LastLng , marker,source){ this.http.get('https://maps.googleapis.com/maps/api/geocode/json?latlng='+LastLat+ &apos ...

What is the process for launching a React/Redux application in development mode?

I am completely new to front-end development. Recently, I was assigned a project that involves ReactJS and Redux. Currently, I am using Visual Studio Code and I need guidance on how to launch the project in dev mode to understand the code better. When I ...

Whenever I attempt to execute a command in cmd, this error consistently pops up

npm ERR! Windows_NT 10.0.18363 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\ADMIN\\AppData\\Roaming\\npm\\node_modules\\np ...

Is it Feasible to Use Component Interface in Angular 5/6?

My main goal is to create a component that can wrap around MatStepper and accept 2..n steps, each with their own components. In other languages, I would typically create an interface with common behavior, implement it in different components, and then use ...