microsoft / microsoft/TypeScript
Proposal: Merge enum and const enum features
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Currently there are two enumerable types specified in TypeScript: enum and const enum.
Both of them aren't bijective, i.e. they both don't provide the ability to cast them arbitrarily and unambiguously between string, number and enum.
After discussing this on Gitter with @jsobell and @masaeedu I'd like to propose the following:
- merge both enumerable types into one: using
const enumon constant index expressions andenumon variable enum expressions. - always return a
numbervalue when astringindex expression is used on anenum. - allow for both,
numberandstringvalues, to be used as index argument types on the proposed mergedenumtype.
This would solve some major problems with the current design. Currently ...
enumvalues cannot be converted to theirnumberequivalent, only to theirstringrepresentation.const enumvalues cannot be converted to theirstringrepresentation, only to theirnumberequivalent.
(This blocksconst enumvalues from being used to serialize configuration settings into a commonly expected string value representation.)const enumvalues can not be converted toenumvalues and vice versa.
The key to providing the missing functionality is type inference. At compile time, TSC is able to tell whether a string or a number value is provided in an enum index argument. It's also able to tell whether the index expression is a constant or a variable.
Given these prerequisites the compiler can easily decide ...
- whether to use a
const enumvalue or anenumin the generated code, - whether to return the numerical value or string representation of the enum in the generated code.
(Variable index expressions of type any should be regarded as values of type string.This will result in maximum compatibility.)
So I'm proposing the following:
- Using a
stringindexer expression on anenumshall return anumberif the type of the L-value isn't the enum itself:enum[:string] => number. - Using a
stringindexer expression on anenumshall return anenumif the type of the L-value is the enum itself:enum[:string] => E. - Using a
numberindexer expression on anenumshall return astringif the type of the L-value isn't the enum itself:enum[:number] => string. - Using a
numberindexer expression on anenumshall return anenumif the type of the L-value is the enum itself:enum[:number] => E. - Using a
enumindexer expression on the sameenumtype shall return anumberif the type of the L-value isn't the enum itself:enum[:enum] => number. (During transpilation, this operation is practically redundant and may be cancelled from the output.) - Using a
enumindexer expression on the sameenumtype shall return anenumif the type of the L-value is the enum itself:enum[:enum] => E. (During transpilation, this operation is practically redundant and may be cancelled from the output.) - Using a constant
stringornumberindexer expression on anenumshall emit a constantnumbervalue in JavaScript (i.e., this is theconst enumequivalent). - Using a variable
stringornumberindexter expression on anenumshall emit an array indexer expression in JavaScript (i.e., this is theenumequivalent).
#### So, given the above prerequisites, here are two examples, hopefully shedding some light upon my proposal:
##### (A) Example illustrating type inference being used at compile time to decide whether to return a `number` or a `string` value from an enum:
The following TypeScript code:
// TypeScript
enum E {a, b, c}
const iN :number = 0, iS1 :string = "b", iS2 :string = "2";
// assigning to primitive types
const s :string = E[iN];
const n1 :number = E[iS1]; // special treatment because index type is string
const n2 :number = E[iS2]; // special treatment because index type is string
// assigning to enum
const es :E = E[iN];
const en1 :E = E[iS1]; // special treatment because index type is string
const en2 :E = E[iS2]; // special treatment because index type is string
... should result in the following JavaScript compilation result:
// JavaScript
var E;
(function (E) {
E[E["a"] = 0] = "a";
E[E["b"] = 1] = "b";
E[E["c"] = 2] = "c";
E.toNumber = function (e)
{ return IsNaN(e) ? E[e] : E.isOwnProperty(e) ? +e : undefined; }
})(E || (E = {}));
var iN = 0, iS1 = "b", iS2 = "2";
var s = E[iN]; // === "a"
var n1 = E.toNumber(iS1); // === 1
var n2 = E.toNumber(iS1); // === 2
var es = E[iN] ? iN : undefined; // E[iN] returns a ?:string. --- result == 0
var en1 = E.toNumber(iS1); // == 1
var en2 = E.toNumber(iS1); // == 2
##### (B) Example illustrating `const enum` and `enum` being merged into one single type:
The following TypeScript code:
// TypeScript
enum E {a, b, c}
let e :E;
const n :number = 1;
const s :string ="c";
// constant assignments
e = E.a;
e = E[2];
e = E["a"];
// variable assignments
e = E[n];
e = E[E[n]];
e = E[s];
... should result in the following JavaScript compilation result:
// JavaScript
var E;
(function (E) {
E[E["a"] = 0] = "a";
E[E["b"] = 1] = "b";
E[E["c"] = 2] = "c";
E.toNumber = function (e)
{ return IsNaN(e) ? E[e] : E.isOwnProperty(e) ? +e : undefined; }
})(E || (E = {}));
var e;
var n = 1;
var s = "c";
e = 0;
e = 2;
e = 0;
e = E[n] ? n : undefined; // E[n] returns a ?:string. --- result == 1
e = E[n] ? n : undefined; // The outer indexing operation is redundant and may be cancelled. --- result == 1
e = E.toNumber(s); // == 2
Some of the `E[n] ? n : undefined` constructs may be cancelled if runtime boundary checking isn't wanted/desired (new boolean compiler option?). So `e = E[n] ? n : undefined;` may then be transpiled to `e = n;`.
In the discussion on Gitter I learned about #3507, #592. Yet I feel the above proposal will add value to the ongoing discussion on improving the `enum` types.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue では TSC、enum のインデックス付け、型推論、JavaScript の出力が挙げられていますが、ファイルやテストは示されていません。まず関連する issue #3507 と #592 を確認し、その後、コンパイラーでの enum のチェックと出力される JavaScript を追跡してください。実装の範囲を定める前に、enum の動作を統合するための合意済みの設計が必要です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 18/100