Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Why are references to other enums never inlined?


Asked by Paola Dudley on Dec 03, 2021 FAQ



References to other enum members are always emitted as property accesses and never inlined. Keep in mind that string enum members do not get a reverse mapping generated at all. In most cases, enums are a perfectly valid solution. However sometimes requirements are tighter.
And,
In this generated code, an enum is compiled into an object that stores both forward (name -> value) and reverse (value -> name) mappings. References to other enum members are always emitted as property accesses and never inlined.
Furthermore, In this generated code, an enum is compiled into an object that stores both forward (name -> value) and reverse (value -> name) mappings. References to other enum members are always emitted as property accesses and never inlined. Keep in mind that string enum members do not get a reverse mapping generated at all.
Also,
In other words, if you were debugging and had to read the runtime value of a numeric enum, the value is often opaque - it doesn’t convey any useful meaning on its own (though reverse mapping can often help), string enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member itself.
Consequently,
Keep in mind that string enum members do not get a reverse mapping generated at all. This means that if you're able, you may want to use string enums, e.g.: enum ObservationType { Accomodation = 'Acc', Addtional = 'Addt', LandTransport = 'LandT', } // Object.keys will only have the three keys.