📖 Scenario: You are creating a simple program to represent different types of user roles in a system. Some roles are identified by numbers, and others by strings. This mix is called a heterogeneous enum.
🎯 Goal: Build a TypeScript heterogeneous enum called UserRole with both numeric and string values, then print one of the roles.
📋 What You'll Learn
Create a heterogeneous enum called UserRole
Include numeric and string values in the enum
Print the value of UserRole.Admin
💡 Why This Matters
🌍 Real World
Heterogeneous enums are useful when you need to represent a set of related constants that have different types, like numeric IDs and string codes, in a clear and organized way.
💼 Career
Understanding enums helps in writing clean, maintainable code in TypeScript, which is widely used in web development and many software projects.
Progress0 / 4 steps
1
Create the heterogeneous enum
Create a heterogeneous enum called UserRole with these exact entries: Admin = 1, Editor = 'EDITOR', Viewer = 3, Guest = 'GUEST'.
Typescript
Hint
Use the enum keyword and assign numbers or strings to each member.
2
Add a variable to hold a role
Create a variable called currentRole and set it to UserRole.Admin.
Typescript
Hint
Use let currentRole = UserRole.Admin; to assign the enum value.
3
Write a function to describe the role
Create a function called describeRole that takes a parameter role of type UserRole and returns a string. Inside, use a switch statement on role with cases for UserRole.Admin, UserRole.Editor, UserRole.Viewer, and UserRole.Guest. Return these exact strings respectively: 'Administrator', 'Content Editor', 'Content Viewer', 'Guest User'. Include a default case returning 'Unknown Role'. Assign the result of describeRole(currentRole) to a variable called roleDescription.
Typescript
Hint
Use a switch statement to return the correct string for each role.
4
Print the role description
Write a console.log statement to print the value of roleDescription.
Typescript
Hint
Use console.log(roleDescription); to print the role description.
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?