【问题标题】:Import statement was not compiled if it was not used later后面不使用import语句不编译
【发布时间】:2018-08-24 02:16:47
【问题描述】:

假设我在分开的文件中定义了两个类,如下所示。

// a.ts 导出默认类 A { } // b.ts 导出默认类 B { }

下面是我使用“a.ts”和“b.ts”的代码。

// 应用程序.ts 从“./a”导入 A; 从“./b”导入 B;

在我用tsc 编译我的源代码后,我发现import 语句没有发送到JavaScript 文件。

// 应用程序.js “使用严格”; Object.defineProperty(exports, "__esModule", { value: true });

但是如果我添加了一些代码来使用AB,它们就会被发出。

// 应用程序.ts 从“./a”导入 A; 从“./b”导入 B; 常量 a = 新 A(); 常量 b = 新 B(); // 应用程序.js “使用严格”; var __importDefault = (this && this.__importDefault) ||功能(模式){ 返回 (mod && mod.__esModule) ?模组:{“默认”:模组}; } Object.defineProperty(exports, "__esModule", { value: true }); const a_1 = __importDefault(require("./a")); const b_1 = __importDefault(require("./b")); const a = new a_1.default(); 常量 b = 新 b_1.default();

我可以更改编译行为,使其始终发出import 语句,无论它是否被使用。

下面是我的tsconfig.json

{ “编译器选项”:{ “目标”:“ES2017”, “模块”:“commonjs”, “严格”:错误, "moduleResolution": "节点", “esModuleInterop”:是的, “experimentalDecorators”:是的, “emitDecoratorMetadata”:真 } }

【问题讨论】:

    标签: typescript


    【解决方案1】:

    这是design,如果不使用模块或仅使用其中的类型,则不会发出它。如果你想为副作用导入一个模块,你应该按照here的建议做一个简单的导入

    import "./a";
    import "./b";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2020-04-12
      相关资源
      最近更新 更多