Heterogeneous enums let you mix numbers and strings in one group. This helps when you want different types of values together but still keep them organized.
Heterogeneous enums in Typescript
enum Example {
First = 1,
Second = "two",
Third = 3
}You can assign numbers or strings to enum members.
Mixing types is allowed but use it carefully to avoid confusion.
enum Status {
Active = 1,
Inactive = "no",
Pending = 3
}enum Response {
Yes = "YES",
No = 0,
Maybe = "MAYBE"
}This program shows how to use a heterogeneous enum and print its values.
enum Mixed {
Ok = 200,
NotFound = "404",
Error = 500
}
console.log(Mixed.Ok); // 200
console.log(Mixed.NotFound); // "404"
console.log(Mixed.Error); // 500Heterogeneous enums can be confusing if overused, so keep them simple.
Use string values when you want readable output, numbers when you need codes.
Heterogeneous enums mix numbers and strings in one enum.
They help organize related but different types of values.
Use them to label options clearly with mixed data types.
Practice
What is a heterogeneous enum in TypeScript?
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 AQuick Check:
Heterogeneous enum = mixed string and number values [OK]
- Thinking enums can only have one type of value
- Confusing heterogeneous with union types
- Assuming enums can't mix strings and numbers
Which of the following is the correct syntax to declare a heterogeneous enum?
enum Status {
Active = 1,
Inactive = "NO"
}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 DQuick Check:
Mixed values in enum = valid heterogeneous enum [OK]
- Assuming enums can't have string values
- Thinking all enum members must be same type
- Confusing enum syntax with object literals
What will be the output of the following TypeScript code?
enum Mixed {
Yes = 1,
No = "NO"
}
console.log(Mixed.Yes);
console.log(Mixed.No);
console.log(Mixed[1]);Solution
Step 1: Check enum values
Mixed.Yes is 1, Mixed.No is "NO".Step 2: Understand reverse mapping
Numeric enums have reverse mapping, so Mixed[1] returns "Yes".Final Answer:
1, NO, Yes -> Option BQuick Check:
Numeric enum reverse lookup works, string does not [OK]
- Expecting string enum members to have reverse mapping
- Confusing enum member names with values
- Assuming Mixed[1] returns 1
Identify the error in this heterogeneous enum declaration:
enum Colors {
Red = "RED",
Green,
Blue = 3
}Solution
Step 1: Check enum member values
Red is assigned a string "RED". Green has no value assigned.Step 2: Understand enum auto-increment rules
After a string member, numeric auto-increment is not allowed. Green must have explicit value.Final Answer:
Green must have an explicit string or number value. -> Option CQuick Check:
Members after string need explicit values [OK]
- Assuming auto-increment works after string member
- Thinking enums can't mix types at all
- Ignoring missing value for Green
You want to create a heterogeneous enum to represent user roles with numeric IDs and string labels. Which enum declaration correctly achieves this?
enum UserRole {
Admin = 1,
Guest = "GUEST",
Member = 2
}Solution
Step 1: Analyze enum member assignments
Admin=1 (number), Guest="GUEST" (string), Member=2 (number). All explicitly assigned.Step 2: Validate heterogeneous enum rules
TypeScript allows explicit mixing of numeric and string values in enums.Final Answer:
This enum is valid and mixes numbers and strings correctly. -> Option AQuick Check:
Explicit assignments allow mixing numbers after strings [OK]
- Thinking numbers cannot follow strings even explicitly
- Confusing explicit assignments with auto-increment rules
- Assuming strict ordering prevents mixing
Ready to prove it?
Pass the test and this pattern moves unknown
