I have created a enum in typescript:
export enum ProductType {
FRUIT,
CLOTH,
}
I wonder if I receive string value fruit
and cloth
, how can I convert the string to the enum type. Here is what I tried:
const value: string = 'fruit';
// compiler error: Element implicitly has an 'any' type because index expression is not of type 'number'
const productType: ProductType = ProductType[value.toUpperCase()];
But I get compiler error pointing to ProductType[value.toUpperCase()]
:
Element implicitly has an 'any' type because index expression is not of type 'number'
What is the right way to convert string to enum type in typescript?