Enum as Parameter in TypeScript

It’s not possible to ensure the parameter is an enum, because enumerations in TS don’t inherit from a common ancestor or interface. TypeScript brings static analysis. Your code uses dynamic programming with Object.keys and e[dynamicKey]. For dynamic codes, the type any is convenient. Your code is buggy: length() doesn’t exists, e[Math.floor((Math.random() * length)+1)] returns a … Read more

How to programmatically enumerate an enum type?

This is the JavaScript output of that enum: var MyEnum; (function (MyEnum) { MyEnum[MyEnum[“First”] = 0] = “First”; MyEnum[MyEnum[“Second”] = 1] = “Second”; MyEnum[MyEnum[“Third”] = 2] = “Third”; })(MyEnum || (MyEnum = {})); Which is an object like this: { “0”: “First”, “1”: “Second”, “2”: “Third”, “First”: 0, “Second”: 1, “Third”: 2 } Enum Members … Read more

Dart How to get the name of an enum as a String

Dart 2.7 comes with new feature called Extension methods. Now you can write your own methods for Enum as simple as that! enum Day { monday, tuesday } extension ParseToString on Day { String toShortString() { return this.toString().split(‘.’).last; } } main() { Day monday = Day.monday; print(monday.toShortString()); //prints ‘monday’ }