microsoft / microsoft/TypeScript

isolatedModules doesn't respect enabled preserveConstEnum option what the project might be build with

Aperta
#37,774 13 commenti 7 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
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

isolatedModules, preserveConstEnum, const enum

Suggestion

The compiler should respect/understand that the package/module was compiled with enabled preserveConstEnums compiler option.

Use Cases

Let's say we have a project written in TypeScript, which uses const enums.

The project uses them because the main const enum's advantage is inlining values instead of runtime accessing to the value.

In other hand the maintainers understand that const enums compiles in nothing in JS code, and that means that consumers can't use a const enums in their JS code (in some cases in TS code either, see above). Thus, they decided to enable preserveConstEnums compiler option to leave all const enums in JS code and allow consumers use them as well.

Up to this moment everything is good. But the next who appears on the stage is isolatedModules (say hello to create-react-app, webpack's transpile-only mode, transpileModule compiler's API and so on). This compiler option is used to detect that imported module could be transpiler without compilation into JS code and everything will work well. One of checks for this option is that you don't import const enums (error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided).

To summarize, if the project/package is compiled with enabled preserveConstEnums, in isolatedModules mode the compiler shouldn't fail with error about unavailability of using const enums, because that's not actually true.

Examples

Quite possible we need to provide that information to the compiler somehow (by providing tsconfig.export.json or in package.json for instance), but I don't even know how it would be done.

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

Inizia tracciando la diagnostica di isolatedModules per i const enums importati e il modo in cui preserveConstEnums viene rappresentato tra la compilazione e i confini dei package. Confronta il percorso della Compiler API tramite transpileModule con la compilazione normale, quindi determina come un package potrebbe comunicare questa impostazione; il lavoro è completato quando il compilatore evita TS2748 solo quando tali informazioni sono disponibili in modo affidabile, con copertura per la configurazione proposta.

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
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.