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

Avatar

By squashlabs, Last Updated: Oct. 14, 2023

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

How to Check if a String is in Enum

Related Article: Tutorial: Navigating the TypeScript Exit Process

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

Tutorial: Working with Dynamic Object Keys in TypeScript

Working with dynamic object keys in TypeScript can be a complex task, but with this step-by-step guide, you'll learn how to manipulate them effective… read more

How to Convert Strings to Booleans in TypeScript

A technical guide on converting strings to booleans in TypeScript. Explore various methods, including using the Boolean() function, the === operator,… read more

How to Set Default Values in TypeScript

Setting default values in TypeScript is an important skill for any developer. This tutorial will guide you through the process, covering topics such … read more

Tutorial: Checking Enum Value Existence in TypeScript

Checking enum value existence in TypeScript is a crucial task for developers. This tutorial provides a step-by-step guide on how to efficiently check… read more

Tutorial on Exact Type in TypeScript

TypeScript is a powerful programming language that introduces static typing to JavaScript. In this tutorial, we will delve into the exact type featur… read more

Working with HTML Button Elements in TypeScript

This tutorial provides a comprehensive guide on working with HTML button elements in TypeScript. From creating and styling buttons to adding event li… read more

How to Iterate Through a Dictionary in TypeScript

Iterating through a dictionary in TypeScript can be done in various ways. This article provides a guide on how to iterate over object keys and values… 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 fo… read more

Comparing Go with TypeScript

An objective analysis of Go and TypeScript, their strengths and weaknesses. The article explores topics such as static typing, type safety, type anno… read more

How to Merge Objects in TypeScript

The tutorial focuses on merging objects in TypeScript, providing a detailed explanation of the techniques involved. It covers various aspects such as… read more