microsoft / microsoft/TypeScript

Generated code when re-exporting a const enum inside a namespace using "preserveConstEnums": true leads to a runtime error

Đang mở
#35,701 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Domain: JS Emit Help Wanted
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện vấn đề với MyEnum.ts, ImportExportInNamespace.ts và App.ts bằng các target es5, commonjs và preserveConstEnums. Trước tiên, hãy theo dõi output do compiler phát ra cho việc export-import namespace, sau đó so sánh với output mong đợi của ImportExportInNamespace.js. Được xem là hoàn tất khi file được tạo bao gồm binding require("./MyEnum") và ví dụ runtime không còn xảy ra lỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.