Why use a mixed enum like this? I didn't realize that was even possible!
As you may be aware, when it comes to number-valued enum members, both the key and the value are stored on the MixedEnum
object. On the other hand, string-valued members only have their key saved. This means that when iterating through the keys, you need to filter out numeric-named members of MixedEnum
. The TypeScript checker provides guidance on how to perform this filtering in an efficient way:
if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) {
error(member.name, Diagnostics.An_enum_member_cannot_have_a_numeric_name);
}
Here's the corresponding function used for filtering:
function isNumericLiteralName(name: string | __String) {
// [comments omitted]
return (+name).toString() === name;
}
With this in mind, the code snippet for iteration would look something like this:
for (let item in MixedEnum) {
if ((+item).toString() === item && 0 * (+item) === 0) continue;
// item represents a key, while MixedEnum[item] corresponds to its value - proceed with processing
}