{

Typescript how to remove Comments in javascript examples | tsconfig.json compiler options removeComments


This tutorial explains about typescript compiler remove comments configuration You can also check how to generate tsconfig.json file and typescript tsconfig.json

tsconfig removeComments option example

Let’s write a typescript code Following code contains

  • simple class with inline and multi-line comments.
  • Easy hello world string print.
// Hello World example
console.log("hello world");

/* Employee class contains 
* Name 
* Salary
* fields
**/
class Employee {
  name?: string; // string or undefined
  salary?: number; // number or undefined
  // constructor
  constructor(name: string, salary: number) {
    this.name = name;
    this.salary = salary;
  }
}

When typescript transpiler to javascript, default comments are added to generate Javascript code.

Now, Let’s change compilerOptions removeComments to true as given below

tsconfig.json Set typescript compiler options removeComments: true

{
  "compilerOptions": {
    "removeComments": true
  }
}

Generated Javascript code as given below

It removes(Strips) comments from the original typescript code.

console.log("hello world");
class Employee {
  name;
  salary;
  constructor(name, salary) {
    this.name = name;
    this.salary = salary;
  }
}

Conclusion

use compileroptions in tsconfig.json,

  • If you want to write comments to Javascript, use removeComments: false
  • use removeComments:false to strip comments in a generated output javascript file

You can use the same setting in Angular and React using typescript projects.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.