Repository navigation
Conversation
…ort declaration When an import declaration has to be rewritten because a runtime value specifier is no longer referenced (for example a class used only as a type), TypeScript no longer elides the remaining specifiers itself. A const enum specifier was treated as a referenced runtime value and kept, although its references are inlined and the module has no such export, so webpack failed with "export ... was not found". Const enum aliases are now handled like type-only aliases unless `preserveConstEnums`, `isolatedModules` or `verbatimModuleSyntax` keeps them. With default options the output now matches what TypeScript emits for the same declaration. Closes angular#34760
There was a problem hiding this comment.
Code Review
This pull request updates the elideImports transformer to correctly elide const enum imports when their references are inlined, unless compiler options like preserveConstEnums, isolatedModules, or verbatimModuleSyntax are enabled. A test case is also added to verify this behavior. The review feedback points out a potential issue where a const enum merged with another value-carrying declaration (such as a namespace) could be incorrectly elided, potentially causing a runtime ReferenceError. A code suggestion is provided to ensure the elision logic is only applied when no other value flags are present on the symbol.
| if ((current.flags & ts.SymbolFlags.ConstEnum) !== 0) { | ||
| return !!( | ||
| compilerOptions.preserveConstEnums || | ||
| compilerOptions.isolatedModules || | ||
| compilerOptions.verbatimModuleSyntax | ||
| ); | ||
| } |
There was a problem hiding this comment.
If a const enum is merged with another value-carrying declaration (such as a namespace), the symbol will have both ts.SymbolFlags.ConstEnum and other value flags (e.g., ts.SymbolFlags.ValueModule).\n\nIn the current implementation, if ts.SymbolFlags.ConstEnum is present, the function immediately returns based on the compiler options, ignoring any other merged value flags. This will cause the import to be elided even if the merged value component (like the namespace) is used at runtime, leading to a runtime ReferenceError.\n\nTo prevent this, we should only apply the const enum elision logic if the symbol does not contain any other value flags.
if (\n (current.flags & ts.SymbolFlags.ConstEnum) !== 0 &&\n (current.flags & (ts.SymbolFlags.Value & ~ts.SymbolFlags.ConstEnum)) === 0\n ) {\n return !!(\n compilerOptions.preserveConstEnums ||\n compilerOptions.isolatedModules ||\n compilerOptions.verbatimModuleSyntax\n );\n }
How I found it
I started from #34760 and checked it against the code. In
elide_imports.ts, once a declaration has to be rewritten because a value specifier is unused (here a class used only as a constructor parameter type), the remaining specifiers are no longer left to TypeScript's own elision.isValueAliastreated the const enum as a runtime value, and because its property access counts as a use, the specifier was kept in the rewritten import. With default options the const enum is inlined and the module has no such export, so webpack reportsexport 'Period' (imported as 'Period') was not found.Where this can show up in practice
@ngtools/webpack/@angular-devkit/build-angular:browserbuild where a component or service imports a const enum together with a class that is only used as a type (for example a DI constructor parameter), as in the repro in @ngtools/webpack keeps aconst enumimport when it rewrites the import declaration ("export ... was not found") #34760.const enumnext to injectable services are a common layout, so more projects on the webpack builder may hit this. This is my estimate; the issue's repro is the only report I know of.PR Checklist
PR Type
What is the current behavior?
When an import declaration is rewritten, a const enum specifier whose references are inlined is still kept, so the emitted import names an export that does not exist at runtime and the webpack build fails.
Issue Number: Closes #34760
What is the new behavior?
isValueAliasnow treats an alias that resolves to a const enum as a non-value alias, unlesspreserveConstEnums,isolatedModulesorverbatimModuleSyntaxkeeps const enum references (the same options TypeScript itself checks). The specifier is then dropped like a type-only one.New test in
elide_imports_spec.ts(emitDecoratorMetadatais false): a declaration that imports a const enum and a type-only class, where the class specifier is removed. The expected output has no import and the inlined value1 /* MixedEnum.A */.Testing
elide_imports_spec.tswith jasmine and TypeScript 6.0.3 together with copies ofelide_imports.tsandspec_helpers.ts:import { MixedEnum } from './mixed') and the other 38 pass;tscemit for the same source with the transformer output:isolatedModulesboth keep it and the reference is not inlined;preserveConstEnumsalonetscdrops the import and the transformer keeps it. The enum object is emitted in that mode, so the import resolves.const enumimport when it rewrites the import declaration ("export ... was not found") #34760 (Angular 22.2.6,@ngtools/webpack22.2.7).ng buildfails with the error above. After applying the same change to the installedelide_imports.js, the build succeeds and the bundle contains"month" /* Period.Month */.prettier --checkpasses on both files. CI here is the real check for the bazel target.Does this PR introduce a breaking change?