'What is the proper way to handle enums in Angular
So I recently started to learn Angular coming from a .NET background and create a small project to test stuff out but one thing I can't seem to make work properly are enums.
I create an class for my enums like this:
export enum ExampleEnum {
Example1 = 0,
Example2 = 1,
Example3 = 2
}
I use this enum in an object which comes from my api and angular maps it like:
ExampleObject {
Id: 1
Name: "Some example"
ExampleEnumType: 0
}
If I want to check something in the code I have to use it like this:
if (ExampleEnum[this.exapleObject.exampleEnumType] === ExampleEnum.Example1) {
// code
}
but this code doesn't always returns a number. Sometimes it returns it as a string and I have to add
as unknown as ExampleEnum as number
This feels really tedious and bad to write it like that.
So my question is how do you handle enums and checks on enums in your code?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
