microsoft / microsoft/TypeScript

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

Aperta
#37,282 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

La issue non indica file né test. Inizia individuando il punto di ingresso del compilatore che emette i const enums quando preserveConstEnums è abilitato, confrontalo con il comportamento esistente dei const enums con valori stringa e segui i test pertinenti dell’emitter; il lavoro è completo quando i const enums numerici vengono emessi senza reverse mappings, mentre gli enum normali mantengono l’output attuale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.