1

I'm trying to convert enum value strings to actual enums. I know you can just compare a string and be done with it (e.g. MyEnum.FirstEnum === 'My_First_Enum' would return true), but it would be nice if I can return enums instead of strings.

export enum MyEnum {
  FirstEnum = 'My_First_Enum',
  SecondEnum = 'My_Second_Enum',
  ThirdEnum = 'My_Third_Enum'
}

getMyEnums(): MyEnum[] {
  // These would be an input argument, but for the sake of this example, I thought this was easier to understand
  const stringEnumValues = ['My_Second_Enum', 'My_Third_Enum'];

  // Convert to enums (result is [undefined, undefined])
  return stringEnumValues.map(e => MyEnum[e]);
}
3
  • From my opinion maybe I am wrong but that's over engineering Commented Jan 21, 2020 at 13:58
  • What's your desired output? Commented Jan 21, 2020 at 14:01
  • @StepUp getMyEnums() should return and array containing SecondEnum and ThirdEnum Commented Jan 21, 2020 at 14:04

3 Answers 3

2

Try to use Object.entries method, then filter by your desired values stringEnumValues and then just map filtered values:

const stringEnumValues = ['My_Second_Enum', 'My_Third_Enum'];  
let result = Object.entries(MyEnum)
                      .filter(([k,v])=> stringEnumValues.includes(v))
                      .map(([ke, vl]) => MyEnum[ke]);

A work example at stackblitz can be seen here.

Sign up to request clarification or add additional context in comments.

1 Comment

One thing I noticed however, is that the compiler thinks "result" is of type any[]. Technically I could just cast stringEnumValues to any[] and return that.
2

You need to cast your string as a key of the enum type, here is how to do it both with an array of strings that represent the enum keys and with an array of strings that represent the values:

Get by keys:

getMyEnums(): MyEnum[] {

  const stringEnumValues = ['SecondEnum', 'ThirdEnum'];  
  return stringEnumValues.map( (e : keyof typeof MyEnum) => MyEnum[e])

}

Get by values:

getMyEnums2(): MyEnum[] {

  const stringEnumValues = ['My_First_Enum', 'My_Second_Enum'];  
  return stringEnumValues.map( (e : MyEnum) => e)

}

Example: https://stackblitz.com/edit/angular-yd6yhe?file=src/app/app.component.ts

Source: https://blog.mikeski.net/development/javascript/typescript-enums-to-from-string/

2 Comments

I'm getting a compile error with the example provided. Using the example from the article I'm getting [undefined, undefined] e.g. return stringEnumValues.map(e => MyEnum[e as keyof typeof MyEnum ]);
Check that, and the examples. Better this way than having to use filter and includes on top of map
0

Why not try a freezed JSON hash for this purpose?

export const MyEnum = Object.freeze({
  'FirstEnum': 'My_First_Enum',
  'SeconfEnum': 'My_Second_Enum',
  'ThirdEnum': 'My_Third_Enum',
  'My_First_Enum': 'FirstEnum',
  'My_Second_Enum': 'SeconfEnum',
  'My_Third_Enum': 'ThirdEnum'
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.