How To Convert a string to Enum in Typescript/Angular
To convert a string to Enum in Typescript or angular follow the below steps.
- Pass the given string to Enum object as a key.
- If the string is part of the enum name entry the value will be returned.
- And then cast it to the enum object to get enum type of string.
Now we will go through an example to understand it further.
I have a created enum object of type Day
, which represents time of Day and a method returnDayOfTime()
.
enum Day {
BeforeNoon = "AM",
AfterNoon = "PM"
}
function returnDayOfTime(day : Day){
console.log(day);
}
I have a string variable which has value BeforeNoon
.
var beforeNoon : string = "BeforeNoon";
We will get the enum value by passing the above string to enum object. (Here I am passing Actual Value)
var enumValue = Day["BeforeNoon"];
If the string key does not exists in enum object it will give Property does not exist on type
error.
var enumValue = Day["BeforeNoon1"];
Property 'BeforeNoon1' does not exist on
type 'typeof Day'. Did you mean 'BeforeNoon'
To understand enum key value pairs please go through the below article.
What if I pass variable instead of actual string.
var enumValue = Day[beforeNoon];
Element implicitly has an 'any' type
because expression of type 'string' can't be used
to index type 'typeof Day'.
To avoid this error we need to cast it to enum object as shown below.
var enumValue = (<any>Day)[beforeNoon];
or
var enumValue : Day = (<any>Day)[beforeNoon];
Now we can pass the above converted enum from string to our method i.e., returnDayOfTime
which accepts enum object as parameter.
returnDayOfTime(enumValue);
//AM
If the string is not part of enum name entry then enumValue will be undefined.
var enumString = "day";
var enumValue : Day = (<any>Day)[enumString];
//undefined