'Typescript: how to compare an enum by name to an enum generated by number
I have the following enum:
export enum MethodType {
First = 1,
Second = 2,
}
An enum variable is set by html content:
let id = document.getElementById("id").getAttribute('data-id');
//id = 2
let methodType = MethodType[id];
Now I try to compare the enum against the enum by name:
console.log(methodType == MethodType.Second);
//returns false
How are enums used in Typescript to compare an enum by name?
The following appears to return true, but Typescript throws an error: This will always return false since the types 'MethodType' and 'string' have no overlap
console.log(methodType == MethodType[MethodType.Second]);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
