Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a .js file generated for a .ts file that only has an interface

Tags:

typescript

Let's say I have a file adder.ts which looks like:

import { Operand } from './operand';

export interface Adder {
  add(op1: Operand, op2: Operand): Operand;
}

And my tsconfig.json looks like:

{
    "compilerOptions": {
        "declaration": false,
        "module": "commonjs",
        "outDir": "./dist",
        "target": "es6",
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ]
}

When I transpile this code I would expect to receive only dist/adder.d.ts. However, there is also a dist/adder.js generated with the contents:

Object.defineProperty(exports, "__esModule", { value: true });

Why is this file generated? What purpose does it serve? At the end of the day I suppose I don't really care much because it would never actually get required/loaded and will be pruned out by any bundler. The only reason I noticed it was because my coverage tool was reporting this file as uncovered.

like image 637
Pace Avatar asked Aug 02 '18 19:08

Pace


Video Answer


1 Answers

Official answer, found by a web search: generating a .js for every .ts simplifies build processes.

A side benefit is that if you do something silly like:

import * as Adder from "./dist/adder";  // or whatever is the correct path
console.log(Adder);

you get an empty namespace instead of a runtime error.

like image 86
Matt McCutchen Avatar answered Oct 08 '22 15:10

Matt McCutchen