Menu
Coddy logo textTech

Using Numeric Enums

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 52 of 73.

You can use an enum as a type annotation just like any other TypeScript type. When you specify an enum as a parameter type, TypeScript ensures that only valid enum values can be passed to that function:

function processUser(role: UserRole): void {
  if (role === UserRole.Admin) {
    console.log("Full access granted");
  } else if (role === UserRole.Editor) {
    console.log("Edit access granted");
  } else {
    console.log("View access only");
  }
}

This approach provides excellent type safety - TypeScript will catch any attempts to pass invalid values, and your code editor will offer autocompletion for the available enum members. You can call this function using processUser(UserRole.Admin) or even processUser(0), since numeric enums accept their underlying numeric values.

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 function named checkPermissions that takes one parameter:

  • role of type UserRole (using the enum from the previous lesson)

The function should:

  • Check the role value and print different messages based on the role
  • If the role is UserRole.Admin, print "Full access granted"
  • If the role is UserRole.Editor, print "Edit access granted"
  • For any other role, print "View access only"
  • Have a return type of void

You are provided with the following from the previous challenge:

  • The UserRole enum with Admin, Editor, and Viewer members
  • Variables: adminRole, editorRole, and viewerRole

Create a variable named currentUser of type UserRole and assign it UserRole.Editor.

Create a variable named guestUser of type UserRole and assign it the numeric value 2.

Print the following outputs by calling your checkPermissions function:

  • Call checkPermissions with adminRole
  • Call checkPermissions with currentUser
  • Call checkPermissions with guestUser
  • Call checkPermissions with UserRole.Admin
  • Call checkPermissions with the numeric value 0

Cheat sheet

Use enums as type annotations for function parameters to ensure type safety:

function processUser(role: UserRole): void {
  if (role === UserRole.Admin) {
    console.log("Full access granted");
  } else if (role === UserRole.Editor) {
    console.log("Edit access granted");
  } else {
    console.log("View access only");
  }
}

You can call enum-typed functions with enum members or their underlying numeric values:

processUser(UserRole.Admin);  // Using enum member
processUser(0);                // Using numeric value

TypeScript provides autocompletion and catches invalid values when using enum type annotations.

Try it yourself

// Provided from previous challenge
enum UserRole {
    Admin,
    Editor,
    Viewer
}

const adminRole = UserRole.Admin;
const editorRole = UserRole.Editor;
const viewerRole = UserRole.Viewer;

// TODO: Write your code here
// Create the checkPermissions function
// Create currentUser variable
// Create guestUser variable
// Call checkPermissions with the required parameters
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