TypeScript: TypeScript removing comments where it shouldn't

comment.ts

if (false) {
    //I will be removed
}
//I will be removed
else {
    //I will not be removed
    var tmp;
    //I will be removed
}

try{
    //I will not be removed
    var tmp;
    //I will be removed
}
//I will be removed
catch(e){
    //I will not be removed
    var tmp;
    //I will be removed
}

comment.js

if (false) {
}
else {
    //I will not be removed
    var tmp;
}
try {
    //I will not be removed
    var tmp;
}
catch (e) {
    //I will not be removed
    var tmp;
}

Are there any additional flags that can be set for TS to keep all comments?

Same issue from CodePlex

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I would like to add another to this list. Let me know if it deserves a separate issue.

TypeScript removes top level comments when a file contains a shebang. We have a company standard that requires copyright statements be added to all of our ts/js files.

Given the typescript file below:

#! /usr/bin/env node
/**
 * @copyright <copyright goes here>
 */
export const foo: string = "bar";

The emitted javascript output is:

#! /usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = "bar";
//# sourceMappingURL=test.js.map

This is not a problem for non-executable javascript files and the header comment is emitted directly below the "use strict";

This is using typescript 2.3.x

TypeScript also appears to be removing comments in this fashion: ts: const foo: boolean = var1 || /* istanbul ignore next */ null;

js: var foo = var1 || null;

This is painful when trying to get code coverage and are setting ignore statements.

I wasn’t suggesting a solution to your problem, I was suggesting that comments at the top of my TS file are being excluded from the JS. E.g., we’re in the same boat - our comments are being stripped!

I have “Keep comments in JavaScript output” checked but my header comment, at the top of my TS is being ignored. Really, I only want to keep comments in the /** Comment */ format for documentation purposes, and this is mostly working. The header comment is something like this:

/** Whatever JavaScript API v1.2.3.4 */
module whatever {
    export class API { ... }
}

The comment on the first line is excluded meanwhile other comments in the file are kept.