Published on

TypeScript Enum Weirdness


Today I was busy trying to debug an issue on the front end where we act on a response from a REST call. One of the values in this response was a Java enum which I had an equivalent enum for on the TypeScript side. I did a switch case on this enum and had different logic based on this switch case. What was weird was none of the branches were being hit.

After further investigation when looking at the enums value in the console I saw that although the TypeScript enum was being assigned a value, it was being assigned a string of the enum value. This all despite the fact the the string was being assigned to a variable that had that enum type (i.e. it was not a string type). This was then being compared to actual enum values in the switch case which was obviously not equal.

I resolved this issue by using a raw string to hold the enum value on the TypeScript side and compared this to string values instead.