Why TS does not use characters for enumerations

ES6 characters are great for enumerations as they avoid collisions. I want the TS type to enumuse characters for enumerations if target:'es6', but it is not:

enum Role {Employee, Manager, Admin}
let role: Role = Role.Employee;

translates to

var Role;
(function (Role) {
    Role[Role["Employee"] = 0] = "Employee";
    Role[Role["Manager"] = 1] = "Manager";
    Role[Role["Admin"] = 2] = "Admin";
})(Role || (Role = {}));
let role = Role.Employee;

any ideas why? With this approach, Role.Employeedoes it matter 0and can any other enumeration with a value 0be used instead Role.Employeeat runtime?

+4
source share
1 answer

ES Symbol , TypeScript enum . - .

enum, . Symbol - . .

, , , enum. Symbol - , . , , /.

- , . , , Node.js Browser, , ( , ). Symbol, , . , , ( Symbol, ).

, , string Symbol .

, TypeScript, enum ambient substitution. , . type erasure enum. , Symbol. Symbol - . , Symbol enum, enum .

, Symbol enum .

+5

Source: https://habr.com/ru/post/1667629/


All Articles