This project is read-only.

Keep one-line-comments (//) in .js and .d.ts

Topics: General
Feb 7, 2014 at 9:45 AM
I'd really like to have my // comments in the resulting .js and .d.ts file, but I couldn't find an option.
/* ... / comments are in the compiled file, but I need also the // comments.
Converting // to /*.. / is not an option.
Syntax:   tsc [options] [file ..]

Examples: tsc hello.ts
      tsc --out foo.js foo.ts
      tsc @args.txt

  -d, --declaration             Generates corresponding .d.ts file.
  -h, --help                    Print this message.
  --mapRoot LOCATION            Specifies the location where debugger should locate map files instead of generated locations.
  -m KIND, --module KIND        Specify module code generation: 'commonjs' or 'amd'
  --noImplicitAny               Warn on expressions and declarations with an implied 'any' type.
  --noResolve                   Skip resolution and preprocessing.
  --out FILE                    Concatenate and emit output to single file.
  --outDir DIRECTORY            Redirect output structure to the directory.
  --removeComments              Do not emit comments to output.
  --sourcemap                   Generates corresponding .map file.
  --sourceRoot LOCATION         Specifies the location where debugger should locate TypeScript files instead of source locations.
  -t VERSION, --target VERSION  Specify ECMAScript target version: 'ES3' (default), or 'ES5'
  -v, --version                 Print the compiler's version:
  -w, --watch                   Watch input files.
  @<file>                       Insert command line options and files from a file.
Feb 7, 2014 at 4:12 PM
The style of comments doesn't add or remove the from the output. Comments are preserved when they are "attached" to syntax elements that are output. For example:
//this is a test
function myFunc() {}
Will output:
//this is a test
function myFunc() {
Because the comment is "attached" to the function myFunc. You could use multi-line comments with the same effect.
Feb 7, 2014 at 6:10 PM
Well, not really.

module M {
    // inline
    export class A {}
     * multiline
    export class B {}
Compilation command line:
tsc --declaration --target ES5 test.ts
declare module M {
    class A {
    * multiline
    class B {
var M;
(function (M) {
    // inline
    var A = (function () {
        function A() {
        return A;
    M.A = A;

    * multiline
    var B = (function () {
        function B() {
        return B;
    M.B = B;
})(M || (M = {}));
You see, the inline comments are in the .js file, but in the .d.ts file, they are removed.
Feb 7, 2014 at 8:20 PM
In a .d.ts only /// or / / comments are preserved. The idea being you can still have comments in your implementation code (using //) that aren't automatically part of the public API/docs when you use --declaration. Instead you use /// and / / comments to be intentional about which bits are your public documentation vs random implementation notes.
Feb 8, 2014 at 10:36 AM
Well that's a good reason! /// comments are only preserved when it is a reference tag. So I'm gonna convert all // to /**/ and everything will work fine. A bit of work for me, but the thinking behind "private" (//) comments is good. One can definitely use that.
Thanks for your answers!
Feb 11, 2015 at 7:55 AM
I know there are a couple discussions out here regarding comment preservation but none seem to directly ask 'which comments are preserved and which are scrapped' when tsc runs?

The comment above about 'attaching' a comment to keep it preserved doesn't seem to work. I'm using TypeScript 1.4 in VS2013 and currently not concerned with/using a .d.ts file. Just .ts and .js. Here are some samples that are baffling me. I just converted my first .js file to *.ts so pardon any incorrect terminology or lack of knowledge. Bottom line, I can't seem to spot a pattern of when tsc is going to decide to keep comments or remove them. I like the idea of public and private comments, but I just need to know comment syntax on how to preserve comments.

Thanks in advance.

            registeredToken = data.RegisteredToken;

            // $(window).focus(GetJson)
            if (runCalculation == undefined || runCalculation == true) {
Result: the *.js file preserved this comment.

    .fail(function (xhr, textStatus, errorThrown) {
        debugger;  // What should we do?
        console.log("Status: " + textStatus);
        $(".ajaxloader").css("display", "none");
Result: the *.js did not preserve this comment. I tried to change it to:
debugger; /* What should we do? */
// What should we do?
then I pasted 'working' comment in there...
            // $(window).focus(GetJson)
        debugger;  // What should we do?
None of these three attempts preserved any comments.

.ts - just a / */ containing a comment regarding some web research
function SaveCalcEngine(location) {
    saveCE = location;
Some versions of IE weren't trusting 'third party headers' - i.e. when myplanchoice posted to secure.benefittech
and every RunCalculation used a different SessionID so we couldn't link up calls in RBL.Evolution service.  These
articles point to work around.
Result: *.js preserved this

*.ts - Another random // comment
    // Always reset, one time use only...
    saveCE = undefined;
    traceEnabled = 0;
Result: *.js preserved this