Type-safe reverse-mapping of enums in Typescript 2.4

299 Views Asked by At

The Typescript 2.4 has introduced String Enums: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html#string-enums

enum Colors {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE",
}

Unfortunately there is also a notice:

... string-initialized enums can’t be reverse-mapped to get the original enum member name. In other words, you can’t write Colors["RED"] to get the string "Red".

The swagger-codegen generates the enums like this.

But how can we map properly from the string variable?

0

There are 0 best solutions below