Imagine you work in a coffee shop and want to write a program to handle coffee orders. You create an enum to list coffee sizes like Small, Medium, and Large. Then, you write a function that takes a size from this enum and gives the correct price.
Using enums helps make sure only the correct sizes are used in the function, so there are no mistakes, like spelling errors. This makes your code easier to read and update later.
In this tutorial, I will explain how to use TypeScript Enums with Functions. Enums in TypeScript are a powerful way to define a set of named constants, making your code more readable and maintainable.
We’ll explore practical examples using USA-specific names to illustrate how you can leverage enums in your TypeScript projects.
What are Enums in TypeScript?
Enums (short for enumerations) allow us to define a collection of related values that can be numeric or string-based. They provide a way to name a set of values, which can make your code more expressive and less error-prone.
Types of Enums:
- Numeric Enums: Default type where values are auto-incremented.
- String Enums: Each member is initialized with a string literal.
- Heterogeneous Enums: A mix of numeric and string values.
Why Use Enums in TypeScript?
Enums help group related constants, making the code easier to read and maintain. They can also represent different states, roles, or types in an application.
Use TypeScript Enums with Functions
Let’s dive into how we can use enums in functions with practical examples.
Example 1: User Roles in a Web Application
Imagine you are developing a web application in which users can have different roles, such as Admin, Editor, and Viewer. You can use an enum to define these roles and write functions that check user permissions.
enum UserRole {
Admin = 'Admin',
Editor = 'Editor',
Viewer = 'Viewer'
}
function checkPermission(role: UserRole): string {
switch (role) {
case UserRole.Admin:
return 'Full access granted.';
case UserRole.Editor:
return 'Edit access granted.';
case UserRole.Viewer:
return 'Read-only access granted.';
default:
return 'No access.';
}
}
// Usage
console.log(checkPermission(UserRole.Admin)); // Output: Full access granted.
console.log(checkPermission(UserRole.Viewer)); // Output: Read-only access granted.In this example, the UserRole enum is used to define user roles. The checkPermission function takes a role as an argument and returns the corresponding permission message.

Example 2: Handling Subscription Plans
Consider a subscription-based service with different plans such as Free, Basic, and Premium. You can use an enum to manage these plans and create functions that handle subscription logic.
enum SubscriptionPlan {
Free = 'Free',
Basic = 'Basic',
Premium = 'Premium'
}
function getSubscriptionBenefits(plan: SubscriptionPlan): string[] {
switch (plan) {
case SubscriptionPlan.Free:
return ['Access to basic features'];
case SubscriptionPlan.Basic:
return ['Access to basic features', 'Priority support'];
case SubscriptionPlan.Premium:
return ['Access to all features', 'Priority support', 'Exclusive content'];
default:
return [];
}
}
// Usage
console.log(getSubscriptionBenefits(SubscriptionPlan.Premium));
// Output: ['Access to all features', 'Priority support', 'Exclusive content']Here, the SubscriptionPlan enum defines the different subscription types. The getSubscriptionBenefits function returns a list of benefits based on the subscription plan.

Combining Enums and Functions for Complex Logic in TypeScript
Enums can be combined with functions to handle more complex logic. Let’s look at an example of managing a task system with different statuses.
Example 3: Task Management System
In a task management system, tasks can have statuses such as To Do, In Progress, and Completed. We can use an enum to represent these statuses and write functions to update and retrieve task information.
enum TaskStatus {
ToDo = 'To Do',
InProgress = 'In Progress',
Completed = 'Completed'
}
interface Task {
id: number;
title: string;
status: TaskStatus;
}
let tasks: Task[] = [
{ id: 1, title: 'Task 1', status: TaskStatus.ToDo },
{ id: 2, title: 'Task 2', status: TaskStatus.InProgress },
{ id: 3, title: 'Task 3', status: TaskStatus.Completed }
];
function updateTaskStatus(taskId: number, status: TaskStatus): boolean {
const task = tasks.find(t => t.id === taskId);
if (task) {
task.status = status;
return true;
}
return false;
}
function getTasksByStatus(status: TaskStatus): Task[] {
return tasks.filter(t => t.status === status);
}
// Usage
updateTaskStatus(1, TaskStatus.InProgress);
console.log(getTasksByStatus(TaskStatus.InProgress));
// Output: [{ id: 1, title: 'Task 1', status: 'In Progress' }, { id: 2, title: 'Task 2', status: 'In Progress' }]In this example, we define a TaskStatus enum and a Task interface. The updateTaskStatus function updates the status of a task, and the getTasksByStatus function retrieves tasks based on their status.

Best Practices for Using Enums in TypeScript
- Use Descriptive Names: Ensure that enum names and their members are descriptive and meaningful.
- Prefer String Enums: String enums are more readable and less prone to errors compared to numeric enums.
- Avoid Heterogeneous Enums: Mixing string and numeric values in an enum can lead to confusion and errors.
- Document Enums: Provide clear documentation for enums to explain their purpose and usage.
Conclusion
Enums in TypeScript are a powerful feature that can enhance the readability and maintainability of your code. By using enums with functions, you can create more structured and understandable code, especially in complex applications. Whether you are managing user roles, subscription plans, or task statuses, enums provide a clear and concise way to handle related constants.
You may like to read:
- Difference Between Enums vs String Literal Unions in TypeScript
- TypeScript Enums vs String Literal Unions
- How to Convert TypeScript Enums to Arrays?
- How to Check if a String is in an Enum in TypeScript?
- How to Check if a Value Exists in an Enum in TypeScript?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.