Create an enum with string values

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Create an enum with string values

Creating an Enum with String Values in TypeScript

Are you tired of using only number values in your enums? Do you want to have more flexibility and use string values instead? Look no further! In this blog post, we will dive into the world of TypeScript enums and explore how to create enums with string values.

The Problem

By default, TypeScript only supports number values in enums. This means that if you try to assign a string value to an enum, you will encounter a compilation error. Let's take a look at an example:

enum e {
    hello = "hello", // error: cannot convert string to e
    world = "world"  // error
};

As you can see, TypeScript raises an error when we try to assign string values directly to the enum. This can be frustrating, especially if you're trying to use enums to represent a specific set of string values.

The Solution

To overcome this limitation, we can use a workaround that involves creating a separate object to map the string values to their corresponding enum values. Let's see how we can do this:

enum e {
    hello,
    world
};

const eMap = {
    hello: e.hello,
    world: e.world
};

In this solution, we define our enum e with number values as usual. Then, we create an object eMap which acts as a mapping between the string values and their corresponding enum values.

Now, we can access the enum values using the string values:

eMap.hello; // Output: 0
eMap.world; // Output: 1

By using this approach, we can effectively mimic an enum with string values in TypeScript.

Summary

Creating enums with string values in TypeScript may seem challenging at first, but with the workaround we've shown, you can easily achieve the same result. By using a separate mapping object, you can associate string values with their corresponding enum values.

Remember, TypeScript enums are a powerful tool to represent a fixed set of values, and by using string values, you can make your code even more expressive and readable.

Now that you know how to create enums with string values, go ahead and try it out in your own projects. Happy coding! 🚀


🌟 Bonus Tip: You can even create a reverse mapping object to get the string value from the enum value if needed. Just keep in mind that this adds more overhead and complexity to your code. Use it wisely!

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my