In this tutorial, I will explain how to check if a string is part of an enum in TypeScript. If you’ve ever worked with enums in TypeScript, you know they are a powerful way to define a set of named constants. However, checking if a string belongs to an enum can be a bit tricky. I will show you how to check if a string is in an enum in TypeScript.
Enums in TypeScript
Enums in TypeScript allow you to define a collection of related values that can be numeric or string-based. They help make your code more readable and maintainable by giving meaningful names to sets of values.
Numeric Enums
Numeric enums are the default in TypeScript. Here’s an example:
enum State {
California = 1,
Texas,
Florida,
NewYork
}In this example, California is assigned the value 1, and the rest of the states are automatically incremented.
String Enums
String enums are more readable as they use string constants instead of numeric values. Here’s an example:
enum City {
LosAngeles = "Los Angeles",
Houston = "Houston",
Miami = "Miami",
NewYorkCity = "New York City"
}In this case, each enum member is initialized with a string value.
Check out How to Split a String by Comma in TypeScript?
Check if a String is in an Enum in TypeScript
There are several ways to check if a string is part of an enum in TypeScript. Let’s explore these methods with practical examples.
Method 1: Using Object.values()
The Object.values() method returns an array of the enum’s values. You can then check if the string exists in this array.
enum City {
LosAngeles = "Los Angeles",
Houston = "Houston",
Miami = "Miami",
NewYorkCity = "New York City"
}
function isCity(value: string): boolean {
return Object.values(City).includes(value);
}
console.log(isCity("Houston")); // true
console.log(isCity("Chicago")); // falseIn this example, Object.values(City) returns an array of city names. The includes() method checks if the provided string is in the array.
Method 2: Using a Type Guard
Type guards can provide a more type-safe way to check if a string is in an enum. Here’s how you can create a type guard for string enums:
enum City {
LosAngeles = "Los Angeles",
Houston = "Houston",
Miami = "Miami",
NewYorkCity = "New York City"
}
function isCity(value: string): value is City {
return Object.values(City).includes(value as City);
}
console.log(isCity("Miami")); // true
console.log(isCity("Boston")); // falseThis method ensures that the function not only checks if the string is in the enum but also narrows the type to the enum type if true.
Read How to Convert an Array to a String in TypeScript?
Method 3: Using an Enum Map
For large enums, creating a map can improve performance by avoiding repeated calls to Object.values().
enum City {
LosAngeles = "Los Angeles",
Houston = "Houston",
Miami = "Miami",
NewYorkCity = "New York City"
}
const cityMap: Record<string, boolean> = Object.keys(City).reduce((acc, key) => {
acc[City[key as keyof typeof City]] = true;
return acc;
}, {} as Record<string, boolean>);
function isCity(value: string): boolean {
return !!cityMap[value];
}
console.log(isCity("New York City")); // true
console.log(isCity("Seattle")); // falseThis approach creates a map where each enum value is a key, allowing for constant-time lookups.
Here is the exact output in the screenshot below:

Check out How to Convert Boolean to String in TypeScript?
Practical Use Case: Validate User Input
Imagine you are building an application where users can select their city from a predefined list. You want to ensure the selected city is valid before processing the data.
Example: User Registration Form
enum City {
LosAngeles = "Los Angeles",
Houston = "Houston",
Miami = "Miami",
NewYorkCity = "New York City"
}
interface User {
name: string;
city: City;
}
function validateUser(user: User): boolean {
if (!isCity(user.city)) {
console.error("Invalid city selected");
return false;
}
return true;
}
const newUser: User = {
name: "John Doe",
city: "Houston" as City
};
if (validateUser(newUser)) {
console.log("User is valid");
} else {
console.log("User is invalid");
}In this example, the validateUser function ensures that the city selected by the user is valid by using the isCity type guard.
Conclusion
In this tutorial, I have explained how to check if a string is a part of an enum in TypeScript using several ways such as using Object.values(), type guards, or an enum map, etc.
You may also like:

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.