Heterogeneous Enums
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 55 of 73.
ypeScript also supports heterogeneous enums - enums that contain a mix of both string and numeric members within the same declaration.
Here's how you can create a heterogeneous enum:
enum MixedEnum {
No = 0,
Yes = "YES",
Maybe = 1
}In this example, No and Maybe are numeric members, while Yes is a string member. TypeScript allows this flexibility, though it's important to note that heterogeneous enums are rarely used in practice.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a heterogeneous enum named ResponseCode that mixes string and numeric values:
Successinitialized to200Errorinitialized to"ERROR"NotFoundinitialized to404Timeoutinitialized to"TIMEOUT"
Create a function named handleResponse that takes one parameter:
codeof typeResponseCode
The function should:
- Check the response code and print different messages based on the value
- If the code is
ResponseCode.Success, print"Request successful" - If the code is
ResponseCode.Error, print"General error occurred" - If the code is
ResponseCode.NotFound, print"Resource not found" - If the code is
ResponseCode.Timeout, print"Request timed out" - Have a return type of
void
Create four variables to demonstrate the enum values:
successCodeof typeResponseCodeand assign itResponseCode.SuccesserrorCodeof typeResponseCodeand assign itResponseCode.ErrornotFoundCodeof typeResponseCodeand assign itResponseCode.NotFoundtimeoutCodeof typeResponseCodeand assign itResponseCode.Timeout
Print the following outputs:
- Print the value of
successCode - Print the value of
errorCode - Print the value of
notFoundCode - Print the value of
timeoutCode - Call
handleResponsewithResponseCode.Success - Call
handleResponsewitherrorCode - Call
handleResponsewith the numeric value404
Cheat sheet
TypeScript supports heterogeneous enums that contain a mix of both string and numeric members within the same declaration:
enum MixedEnum {
No = 0,
Yes = "YES",
Maybe = 1
}In heterogeneous enums, you can mix numeric and string values. While TypeScript allows this flexibility, heterogeneous enums are rarely used in practice.
Try it yourself
// TODO: Write your code here
// Create the ResponseCode enum
// Create the handleResponse function
// Create the demonstration variables
// Print the required outputsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos8Enums
What is a Numeric Enum?Using Numeric EnumsWhat is a String Enum?Using String EnumsHeterogeneous EnumsRecap: Using Enums3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples