microsoft / microsoft/TypeScript
Don't emit `const enum`s with reverse mapping (under `preserveConstEnum`)
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue ではファイルもテストも指定されていません。まず、preserveConstEnums が有効なときに const enums を出力するコンパイラーのエントリーポイントを特定し、既存の文字列値の const enums の動作と比較して、関連する emitter テストを追跡してください。数値の const enums が reverse mappings なしで出力され、通常の enum が現在の出力を維持すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100