microsoft / microsoft/TypeScript

`isolatedModules` does not generate the same code

Open
#60,110 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Docs
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and compare the emitted code with and without isolatedModules for the const enum example. Trace the compiler path responsible for const enum handling and determine whether values should be inlined or an error should be reported; done means matching output or a clear diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.