microsoft / microsoft/TypeScript

`isolatedModules` does not generate the same code

Offen
#60,110 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Docs
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

isolatedModules
const enum
inlining
generated code

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play/?preserveConstEnums=true&module=5&isolatedModules=true&ts=5.7.0-dev.20240930#code/FAYw9gdgzgLgBAUwgVwLZwHJhgZTAQQHcEoxUEBhSWAURXQG9g449yA1AQwBtkEAaZnHwRsACwQAnAPIQEwAL7BlAM2QQQMAJaQ4AEzClyMMVogBzABSxOMBAC5M2PERJlK1GHTQBKRwDcwLT04JhYoQi0YEDE4axhbBB9QoRYQTigEJ1wCYiMPaC96ADo2BC5eB1SWOHBoMG4EYu4wKwByMoq+Np8AbmqWACNJBE4Aa36a2oysrBzXfKpC71RikXEpWSqptOoGppb29ZNNuR7JneHRiaElBSA

💻 Code
const enum NotSoAwesomeConstEnum {
  SomeValue,
  AnotherOne
}


function dosomething(state: NotSoAwesomeConstEnum): void {
  switch (state) {
    case NotSoAwesomeConstEnum.SomeValue:
      console.log('SomeValue');
      break;
    case NotSoAwesomeConstEnum.AnotherOne:
      console.log('AnotherOne');
      break;
  }
}
🙁 Actual behavior

The emitted code is not the same when using isolatedModules.

🙂 Expected behavior

The emitted code should be the same when using isolatedModules as it is described as nothing else but a check

Setting the isolatedModules flag tells TypeScript to warn you if you write certain code that can’t be correctly interpreted by a single-file transpilation process.

const enum values should be inlined even when using this flag. (Or an error should be emitted).

Additional information about the issue

From the documentation:

It does not change the behavior of your code, or otherwise change the behavior of TypeScript’s checking and emitting process.

Which is not true, at least for const enum as this prevents inlining.
See the playground link and https://github.com/microsoft/TypeScript/issues/16671#issuecomment-1686858424

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften TypeScript Playground und vergleiche den ausgegebenen Code mit und ohne isolatedModules für das const enum-Beispiel. Verfolge den für die Behandlung von const enum zuständigen Compilerpfad und ermittle, ob Werte inline eingesetzt oder ein Fehler gemeldet werden sollten; abgeschlossen ist die Aufgabe, wenn die Ausgabe übereinstimmt oder eine eindeutige Diagnose vorliegt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.