How to Check if a String is in Enum in TypeScript: A Tutorial

Avatar

By squashlabs, Last Updated: October 13, 2023

How to Check if a String is in Enum in TypeScript: A Tutorial

How to Check if a String is in Enum

In TypeScript, an enum is a way to define a set of named constant values. It allows you to create a group of related values that can be assigned to a variable or used as a type. Sometimes, you may need to check if a given string value exists in an enum. TypeScript provides several methods to achieve this.

One way to check if a string is in an enum is by using the keyof operator and the in operator. The keyof operator returns the union of all property names of a type, and the in operator checks if a property exists in an object.

Here’s an example that demonstrates how to check if a string is in an enum:

enum Fruit {
Apple = "apple",
Banana = "banana",
Orange = "orange"
}

function isFruit(value: string): value is Fruit {
return value in Fruit;
}

console.log(isFruit("apple")); // Output: true
console.log(isFruit("grape")); // Output: false

In this example, we have an enum called Fruit with three values: Apple, Banana, and Orange. The isFruit function takes a string parameter and checks if the value exists in the Fruit enum using the in operator. The function returns a boolean indicating whether the value is a valid fruit.

Another way to check if a string is in an enum is by using the Object.values method. This method returns an array of all enum values. You can then use the includes method to check if the given string value is present in the array.

Here’s an example that demonstrates this approach:

enum Color {
Red = "red",
Blue = "blue",
Green = "green"
}

function isColor(value: string): value is Color {
return Object.values(Color).includes(value);
}

console.log(isColor("red")); // Output: true
console.log(isColor("yellow")); // Output: false

In this example, we have an enum called Color with three values: Red, Blue, and Green. The isColor function takes a string parameter and checks if the value exists in the Color enum by using Object.values to get an array of all enum values and then using the includes method to check if the value is present in the array.

These are two common methods to check if a string is in an enum in TypeScript. Choose the method that best suits your needs and the specific requirements of your project.

External sources:
– TypeScript documentation on enums: https://www.typescriptlang.org/docs/handbook/enums.html
– MDN web docs on the in operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

How to Implement and Use Generics in Typescript

Generics in TypeScript provide a powerful way to write reusable and type-safe code. This tutorial will guide you through the syntax, concepts, and practical use cases of... read more

How Static Typing Works in TypeScript

TypeScript is a powerful programming language that offers static typing capabilities for better code quality. In this comprehensive guide, we will explore various... read more

How to Verify if a Value is in Enum in TypeScript

This article provides a guide on how to check if a specific value exists in an Enum using TypeScript. It covers understanding Enums in TypeScript, validating Enum... read more

Tutorial: Converting a String to Boolean in TypeScript

Converting a TypeScript string into a boolean can be a tricky task. This tutorial provides different approaches, code snippets, and best practices for handling this... read more

How to Update Variables & Properties in TypeScript

Updating variables and properties in TypeScript can be a simple and process. This step-by-step tutorial will guide you through the correct usage of the SetValue function... read more

Tutorial on Prisma Enum with TypeScript

Prisma Enum is a powerful feature in TypeScript that allows you to define and use enumerated types in your Prisma models. This tutorial will guide you through the... read more