Skip to content
Advertisement

Tag: enums

Get TypeScript enum numeric value by member name

I’m converting one of my JavaScript apps to TypeScript and I’ve got a problem: I’ve got a numeric enum, AnswerKeys and a key, “C”. I’d like to get the member’s value, “2”, so I tried using AnswerKeys[key]. It would work but I receive the following TypeScript error: Element implicitly has an ‘any’ type because index expression is not of type

Enums in Javascript with ES6

I’m rebuilding an old Java project in Javascript, and realized that there’s no good way to do enums in JS. The best I can come up with is: The const keeps Colors from being reassigned, and freezing it prevents mutating the keys and values. I’m using Symbols so that Colors.RED is not equal to 0, or anything else besides itself.

const enum in Typescript

I have a React application that is using Typescript. Right now I’m running into an issue with const enum. Here’s my enum: The service I’m trying to match up to isn’t returning the value, but the index of the item within the enum. So, for instance, if the user is set to snack on an apple, the service is returning

Advertisement