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.
numberswork, butstringsdon't. I don't have them mixed. I also don't see how this is relevant?