Define enum like this :
Then you can use this :
If you need enum values to hold properties, you can add them to an extra object:
Then you can like this:
Here we can define the enum like this:
This doesn't prevent you from assigning an undesired value to a variable, which is often the main goal of enums:
let day = DaysEnum.tuesday day = 298832342 // goes through without any errors
Object.defineProperty able to call and define enum values for any object, without affecting an other attributes.
writable:false type is safe. We should able to create a custom object, then call
Enum() on it. The values assigned start at 0 and increment per data.
Symbol() != Symbol(). For example:
Read AlsoJava Enum tutorial
To add a description to enum values:
To simplifies this code is required to initialize the enum :
This solution is implemented by TypeScript interface:
This enables to look up on both
MyEnum.Techy which returns 1, and
MyEnum which returns "Techy" regardless of the order of declaration.