0
votes

Let's say I have two classes defined in separated files as below.

// a.ts
export default class A {
}
// b.ts
export default class B {
}

Below is the code I'm using "a.ts" and "b.ts".

// app.ts
import A from "./a";
import B from "./b";

After I compiled my source code with tsc I found the import statements were not emitted to JavaScript file.

// app.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

But if I added some code to use A and B, they will be emitted.

// app.ts
import A from "./a";
import B from "./b";

const a = new A();
const b = new B();
// app.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
}
Object.defineProperty(exports, "__esModule", { value: true });
const a_1 = __importDefault(require("./a"));
const b_1 = __importDefault(require("./b"));
const a = new a_1.default();
const b = new b_1.default();

May I change the compilation behaviour so that it will be always emitting the import statement regardless if it's used or not.

Below is my tsconfig.json.

{
  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "strict": false,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}
1

1 Answers

1
votes

This is by design, if a module is not used or only types from it are used, it will not be emited. If you want to import a module for the side effects you should do a simple import as suggested here

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