A heterogeneous enum is an enum that contains both string and numeric values in its members.
Click to reveal answer
intermediate
Why might you use a heterogeneous enum?
You might use a heterogeneous enum to represent values that need both numbers and strings, for example, when some values are codes and others are descriptive labels.
Click to reveal answer
beginner
How do you declare a heterogeneous enum in TypeScript?
You declare it by assigning some members string values and others numeric values explicitly inside the enum block.
Click to reveal answer
beginner
Example: What is the output of this enum member access?
enum Example {
No = 0,
Yes = "YES"
}
console.log(Example.Yes);
The output will be the string "YES" because the member Yes is assigned the string value "YES".
Click to reveal answer
intermediate
Can heterogeneous enums cause any issues in TypeScript?
Yes, they can make code harder to understand and maintain because the enum members are not consistent in type, which can confuse type checking and usage.
Click to reveal answer
Which of the following is a valid heterogeneous enum in TypeScript?
Aenum Mixed { A = 1, B = 2 }
Benum Mixed { A, B, C }
Cenum Mixed { A = "A", B = "B" }
Denum Mixed { A = 1, B = "B" }
✗ Incorrect
Only option A mixes numeric and string values, making it a heterogeneous enum.
What type of values can heterogeneous enums contain?
ABoth numbers and strings
BOnly strings
COnly numbers
DOnly booleans
✗ Incorrect
Heterogeneous enums contain both numeric and string values.
What will be the output of this code?
enum Status {
Active = 1,
Inactive = "NO"
}
console.log(Status.Inactive);
A1
B"NO"
Cundefined
D0
✗ Incorrect
Status.Inactive is assigned the string "NO", so that is printed.
Is this enum valid?
enum Test {
One = "1",
Two = 2
}
ANo, enums cannot mix types
BNo, string values are not allowed
CYes, it's a heterogeneous enum
DYes, but only if all values are strings
✗ Incorrect
TypeScript allows enums to mix string and numeric values, making this a valid heterogeneous enum.
What is a potential downside of using heterogeneous enums?
AThey can confuse type checking and maintenance
BThey are slower to compile
CThey cannot be used in switch statements
DThey always cause runtime errors
✗ Incorrect
Mixing types in enums can make code harder to understand and maintain.
Explain what a heterogeneous enum is and give a simple example.
Think about enums that mix numbers and strings.
You got /2 concepts.
Describe one advantage and one disadvantage of using heterogeneous enums.
Consider why mixing types might help or hurt your code.
You got /2 concepts.
Practice
(1/5)
1.
What is a heterogeneous enum in TypeScript?
easy
A. An enum that contains both string and numeric values.
B. An enum that only contains numeric values.
C. An enum that only contains string values.
D. An enum that cannot be used with functions.
Solution
Step 1: Understand enum value types
Enums in TypeScript can hold numeric or string values.
Step 2: Define heterogeneous enum
A heterogeneous enum mixes both string and numeric values in the same enum.
Final Answer:
An enum that contains both string and numeric values. -> Option A
Quick Check:
Heterogeneous enum = mixed string and number values [OK]
Hint: Remember: mixed types in one enum means heterogeneous [OK]
Common Mistakes:
Thinking enums can only have one type of value
Confusing heterogeneous with union types
Assuming enums can't mix strings and numbers
2.
Which of the following is the correct syntax to declare a heterogeneous enum?
enum Status {
Active = 1,
Inactive = "NO"
}
easy
A. Incorrect because enum members must be all strings.
B. Incorrect because enums cannot have string values.
C. Incorrect because enum members must be all numbers.
D. Correct syntax with mixed number and string values.
Solution
Step 1: Check enum member types
The enum has one numeric value (1) and one string value ("NO").
Step 2: Validate TypeScript enum rules
TypeScript allows heterogeneous enums mixing numbers and strings.
Final Answer:
Correct syntax with mixed number and string values. -> Option D
Quick Check:
Mixed values in enum = valid heterogeneous enum [OK]
Hint: Look for mixed number and string values in enum members [OK]
Common Mistakes:
Assuming enums can't have string values
Thinking all enum members must be same type
Confusing enum syntax with object literals
3.
What will be the output of the following TypeScript code?