Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a heterogeneous enum named ResponseCode that mixes string and numeric values:

  • Success initialized to 200
  • Error initialized to "ERROR"
  • NotFound initialized to 404
  • Timeout initialized to "TIMEOUT"

Create a function named handleResponse that takes one parameter:

  • code of type ResponseCode

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:

  • successCode of type ResponseCode and assign it ResponseCode.Success
  • errorCode of type ResponseCode and assign it ResponseCode.Error
  • notFoundCode of type ResponseCode and assign it ResponseCode.NotFound
  • timeoutCode of type ResponseCode and assign it ResponseCode.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 handleResponse with ResponseCode.Success
  • Call handleResponse with errorCode
  • Call handleResponse with the numeric value 404

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 outputs
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript