How to Convert a String to enum in TypeScript?
In TypeScript, an enum is a type of class that is mainly used to store the constant variables with numerical and string-type values. In this article, we will learn, how we can convert a string into an enum using TypeScript.
These are the two approaches that can be used to solve it:
Table of Content
- Using custom mapping
- Using the keyof and typeof operators together
- Using type assertion
- Using a generic function
Using custom mapping
In this approach, we will define an enum initialized with string values and then map through each item to compare them with a string and return the matching enum value to convert the string into an enum.
Example: The below example will explain how you can convert a string into an enum using custom mapping.
enum GFG {
name = "w3wiki",
desc = "A Computer Science portal."
}
// Function to convert string into enum
function convertStrToEnum(convertingStr: string):
GFG | string {
switch (convertingStr) {
case "w3wiki":
return GFG.name;
case "A Computer Science portal.":
return GFG.desc;
default:
return `Pass either "${GFG.name}" or "${GFG.desc}"
as testing string to the function`;
}
}
console.log(convertStrToEnum("w3wiki"));
console.log(convertStrToEnum("TypeScript"));
console.log(convertStrToEnum("A Computer Science portal."));
Output:
w3wiki
Pass either "w3wiki" or "A Computer Science portal." as testing string to the function
A Computer Science portal.
Using the keyof and typeof operators together
The keyof and the typeof operators can together be used to convert an string into an enum in TypeScript.
Syntax:
const variable_name: keyof typeof enum_name = value;
Example: The below example will explain the use of the keyof and typeof operators to convert a string into an enum.
enum GFG {
name = 25,
desc = 56
}
// Converting string to enum
const myStr: keyof typeof GFG = 'name';
const myStr1: keyof typeof GFG = 'desc';
// It prints 25, as the string is now converted
// to the value of first constant of enum
console.log(GFG[myStr]);
// It prints 56, as the string is now converted
// to the value of second constant of enum
console.log(GFG[myStr1]);
Output:
25
56
Using type assertion
In this method, we will convert a string to an enum by using the unknown type assertion at the time of conversion.
Syntax:
const variable_name1: string = value_as_enum_key_as_string;
const variable_name2 = variable_name1 as unknown as enum_name;
Example: The below code example illustrate the type assertion approach to convert a string into enum using TypeScript.
enum GFG {
num1 = 28,
num2 = 56,
num3 = 84
}
// Assigning enum values to
// string type variables
const str1: string = 'num1';
const str2: string = 'num2';
const str3: string = 'num3';
// Converting String into enum
const str1ToEnum = str1 as unknown as GFG;
const str2ToEnum = str2 as unknown as GFG;
const str3ToEnum = str3 as unknown as GFG;
console.log(GFG[str1ToEnum]);
console.log(GFG[str2ToEnum]);
console.log(GFG[str3ToEnum]);
Output:
28
56
84
Using a generic function
In this approach, we’ll create a generic function that ensures type safety during the conversion of a string to an enum. The function checks if the provided string matches any of the enum values and returns the corresponding enum value or a message indicating the valid options.
Syntax:
function convertStrToEnum<T extends keyof typeof enum_name>(convertingStr: string): enum_name |
string {
// Implementation
}
Example: The following example demonstrates the usage of a generic function approach to convert a string into an enum.
enum GFG {
name = "w3wiki",
desc = "A Computer Science portal."
}
// Generic function to convert string into enum
function convertStrToEnum<T extends keyof typeof GFG>(convertingStr: string): GFG
| string {
if (Object.values(GFG).includes(convertingStr as GFG)) {
return convertingStr as GFG;
} else {
return `Pass either "${GFG.name}" or
"${GFG.desc}" as testing string to the function`;
}
}
console.log(convertStrToEnum("w3wiki"));
console.log(convertStrToEnum("TypeScript"));
console.log(convertStrToEnum("A Computer Science portal."));
Output:
w3wiki
Pass either "w3wiki" or "A Computer Science portal." as testing string to the function
A Computer Science portal.