1

I'm using TypeScript, and have the following test Enum

enum Colors {
    Red = "RED",
    Green = "GREEN",
    Blue = "BLUE",
    Other = 1
}

Note: This enum is just for demonstration. I don't actually mix numbers and strings, but it's just easier than to write two enums just to show what's wrong.

Now, in my code, I want to check if a specific value is in the enum, however, when checking against string Enums it's always false

'RED' in Colors // false
"RED" in Colors // false
'GREEN' in Colors // false
"GREEN" in Colors // false
1 in Colors // true

I checked the compiled code, and this is how it looks

var Colors;
(function (Colors) {
    Colors["Red"] = "RED";
    Colors["Green"] = "GREEN";
    Colors["Blue"] = "BLUE";
    Colors[Colors["Other"] = 1] = "Other";
})(Colors || (Colors = {}));

Isn't it quite strange that the generated code for an integer looks different than for a string?

My TypeScript version is 2.4.1 and according to this blog, it's available since 2.4, so I should be good to go, right?

I tried to only use one value (string/number) in the enum, but still the same errors. How can I check if a value exists in a string based Enum?

I compile my code to ES5, but with ES6 the same problem occurs.

2
  • Why would you want to mix number/string values to begin with? Commented Jun 29, 2017 at 10:52
  • Was just an example to show that numbers work, but strings don't. I don't have them mixed. I also don't see how this is relevant? Commented Jun 29, 2017 at 10:53

1 Answer 1

3

You are accessing by property value, but, instead, in operator is used to access by property name.

your data looks like:

var Colors = {
  Red: "RED"
}

so you can test if the key Red is in Colors: 'Red' in Colors // true

if you need to check if one of the keys has "RED" (all upper) as value, then you need to iterate over alle the object.

var Colors = {
  Red: "RED"
}

function valueInColors(needed) {
  for(let k in Colors) {
    if(Colors.hasOwnProperty(k) && Colors[k] === needed) {
      return true;
    }
  }
  
  return false;
}

console.log(valueInColors('RED'))

Ok, so you mean, when I'm searching for 1 in Colors it doesn't find the element with the value 1, but instead it finds the element with the key 1 in the generated array?

Colors[Colors["Other"] = 1] = "Other";

// Assignment operator returns the value, so, the previous expression is like:

Colors["Other"] = 1
Colors[1] = "Other";

Ah, I understand. So technically speaking, I don't even need the values? I don't need to use "Red: Red, Green: Green", I could just go with "Red, Green", etc.?

Sometimes you need, sometimes no. In this specific case I think you don't need values. You can find further info here: https://www.typescriptlang.org/docs/handbook/enums.html

Sign up to request clarification or add additional context in comments.

7 Comments

Okay, but why does it work that way for strings, but not for integers? I can check 1 in Colors and it returns true. For example in this answer: stackoverflow.com/questions/43804805/… They show how to search for value, not by property name. Same for this answer: stackoverflow.com/questions/40598296/… It shows for getting a value
Thanks for your answer. Upvoted you. I'm still wondering, however, why it works different for integers.
They are not looking by value, all the example you have linked are looking by key, the thing is that Enums are also indexed by int: typescriptlang.org/docs/handbook/enums.html
Ok, so you mean, when I'm searching for 1 in Colors it doesn't find the element with the value 1, but instead it finds the element with the key 1 in the generated array?
Thanks, but what I'm not getting is why it's different for a string? Why isn't it `Colors[Colors["RED"] = 'Red'] = 'Red' and then I have both as the key and value?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.