microsoft / microsoft/TypeScript

Don't emit `const enum`s with reverse mapping (under `preserveConstEnum`)

Đang mở
#37,282 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

Search Terms

preserveConstEnums, const enum, enum

Suggestion

By default all const enums are preserved from output. One of the key differences of const enums is disallowing to looking up the reversed value:

enum Enum {
  Zero,
  One,
}

console.log(Enum[1]); // allowed, result is 'One'

const enum ConstEnum {
  Zero,
  One,
}

console.log(ConstEnum[1]); // Error

This means that we can't access const enum's "reversed" member even if preserveConstEnums flag is enabled.

JS code of const enum with enabled preserveConstEnums is pretty similar to just enums right now and contains string literals (even they couldn't be accessed from the code):

var ConstEnum;
(function (ConstEnum) {
    ConstEnum[ConstEnum["Zero"] = 0] = "Zero";
    ConstEnum[ConstEnum["One"] = 1] = "One";
})(ConstEnum || (ConstEnum = {}));

My feature request is change output of const enums when preserveConstEnums is enabled and strip "reversed" values from it:

var ConstEnum;
(function (ConstEnum) {
    ConstEnum["Zero"] = 0;
    ConstEnum["One"] = 1;
})(ConstEnum || (ConstEnum = {}));

Note: actually tsc already has similar behaviour if you specify string constant value for every const enum's member, but the values are strings, not numbers - it is kind of workaround if your const enum is used to declare string constants.

Use Cases

Reversed values for const enums are useless and cannot be accessed in the TS code, so why we should emit them? 🤔 I guess in this case the behaviour of const enums from types and from execution (JS) purposes will be the same.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue không nêu tệp hoặc test nào. Hãy bắt đầu bằng cách tìm entry point của compiler phát ra const enums khi preserveConstEnums được bật, so sánh với hành vi hiện có của const enums có giá trị chuỗi và lần theo các test emitter liên quan; được xem là hoàn tất khi const enums dạng số được phát ra mà không có reverse mappings, trong khi các enum thông thường vẫn giữ nguyên output hiện tại.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.