microsoft / microsoft/TypeScript
Generated code when re-exporting a const enum inside a namespace using "preserveConstEnums": true leads to a runtime error
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.8.0-dev.20191216
Search Terms: const enum import export preserveConstEnums
Code
MyEnum.ts
const enum MyEnum {
FirstValue,
SecondValue
}
export default MyEnum;
ImportExportInNamespace.ts
import _MyEnum from "./MyEnum";
export namespace MyNamespace {
export import MyEnum = _MyEnum;
}
App.ts
import { MyNamespace } from "./ImportExportInNamespace";
console.log(MyNamespace.MyEnum.FirstValue);
Compile the above using
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"preserveConstEnums": true
}
}
Expected behavior:
ImportExportInNamespace.ts compiles to
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MyEnum_1 = require("./MyEnum");
var MyNamespace;
(function (MyNamespace) {
MyNamespace.MyEnum = MyEnum_1.default;
})(MyNamespace = exports.MyNamespace || (exports.MyNamespace = {}));
Actual behavior:
ImportExportInNamespace.ts wrongly compiles to
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MyNamespace;
(function (MyNamespace) {
MyNamespace.MyEnum = MyEnum_1.default;
})(MyNamespace = exports.MyNamespace || (exports.MyNamespace = {}));
i.e. we are missing
var MyEnum_1 = require("./MyEnum");
If I change the enum not to be a const the code compiles correctly. I'm using it as a workaround.
Related Issues:
#23514
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with MyEnum.ts, ImportExportInNamespace.ts, and App.ts using target es5, commonjs, and preserveConstEnums. Start by tracing the compiler's emitted output for the namespace export-import, then compare it with the expected ImportExportInNamespace.js output. Done means the generated file includes the require("./MyEnum") binding and the runtime example no longer errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100