24

I like to have my comments intact in the resulting javascript file, by default the compiler removes them. Is there a tsc parameter for that? (The use case is to keep /// reference path's = ... for chutzpah unit testing. )

joeriks
  • 3,382
  • 8
  • 32
  • 42
  • Thanks for the answers, I also found the option in Web Essentials Tools>Options>Web Essentials> Keep Comments , now testing is easy : http://joeriks.com/2012/10/06/testing-typescript-with-chutzpah/ – joeriks Oct 06 '12 at 18:34
  • Now Chutzpah supports Typescript ootb which is great, see answer by Matthew Manela below. – joeriks Oct 29 '12 at 15:49

6 Answers6

25

Since 2015 you can create a tsconfig.json in your project and add "removeComments": false to its "compilerOptions" property in order to keep your comments in the resulting javascript files.


1. Create the tsconfig.json file for your project from your terminal (if necessary)

tsc -init

2. Add "removeComments": false to your tsconfig.json file inside the "compilerOptions" property

At the end, you should expect your tsconfig.json file content to be like this:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "removeComments": false
    },
    "exclude": [
        "node_modules"
    ]
}

3. Compile your .ts file into a .js file from your terminal

  • Use tsc myFile.ts in order to keep your comments
  • Use tsc --removeComments myFile.ts in order to remove your comments

You can learn more about tsconfig.json compiler options on Typescriptlang.org tsconfig.json page.

Furthermore, according to the Typescript documentation, setting true or false to the "removeComments" property will have no effect on copy-right header comments beginning with /*!. Thus, they will always appear in your .js files.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
  • This won't work for type/code not going to be emitted in the JS output: https://github.com/microsoft/TypeScript/issues/1665 – Irfan Jul 02 '19 at 23:55
17

Comments that start with /*! are preserved.

example:
/*! this comment remains untouched */
/* but this one will be removed */

amin
  • 3,672
  • 5
  • 33
  • 61
11

Yes, the -c (or --comments) option;

Syntax: tsc [options] [file ..]

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

Options:
   -c, --comments        Emit comments to output
...

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 2
    Which version introduces it? I get `error TS5023: Unknown compiler option 'comments'.` for `1.0.3.0` and `1.5.0-alpha` – tomalec Apr 05 '15 at 19:13
  • Comments starting with a !, such as /*! comment */ are preserved. This is helpful when you want only some comments to remain in the js transpiled file – EuberDeveloper Dec 15 '19 at 12:40
8

Currently using 1.6.2 and it appears comments are preserved by default. The only comment-related flag in the compiler is to remove comments. As per the docs:

--removeComments
Remove all comments except copy-right header comments beginning with /!*

Jonathan Guerrera
  • 971
  • 1
  • 9
  • 15
4

You will have to edit the underlying .csproj file and include the -c option.
Have a look here:

http://blorkfish.wordpress.com/2012/10/06/including-typescript-comments-in-generated-javascript/

user229044
  • 232,980
  • 40
  • 330
  • 338
blorkfish
  • 21,800
  • 4
  • 33
  • 24
0

Chutzpah 2.2 now supports TypeScript natively so you don't need to worry about this. You can run Chutzpah directly on the .ts file and it will run your tests.

Matthew Manela
  • 16,572
  • 3
  • 64
  • 66