Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there TypeScript-only comments that don't get written into the .js?

Tags:

typescript

If I add a regular comment (either /* */ or //) to a .ts file, it gets written to the output js file unless I specify removeComments during the compilation.

However, I wonder if it's possible to do it the other way round, keep comments except for "TypeScript-only" comments. This would be similar to how the Razor View Engine for ASP.net allows to use @* Comment *@ to indicate comments.

I know I could use copyright comments (/*!) as regular comments and javascript comments as TypeScript only comments (and this remove them with removeComments), but that seems really awkward and hard to remember.

Without the use of additional build tools/processing, does the TypeScript compiler have a way that I've overlooked, or is removeComments the only applicable choice here?

like image 870
Michael Stum Avatar asked Dec 19 '18 23:12

Michael Stum


People also ask

What are comments in typescript?

Typescripts comments: Learn Inline single, multiline, and documentation comments Comments are code statements that are ignored by Typescript during the compilation process. Every programming language provides comments to describe the line of code or functions.

Does TypeScript support type checking JavaScript files?

However, since TypeScript 2.3, TypeScript and VsCode support type checking JS files using JSDoc. This doesn’t add any delays as nothing is transpiled, and it’s completely optional! In the official TypeScript documentation, you can find a section on Type checking JavaScript files.

Do people actually like typescript?

The only people who like TypeScript are those who don’t understand the most basic concepts of JavaScript. “Oh, it doesn’t have types” – No shit, buddy! And that’s not an accidental bug, it’s a core feature!

Why use allowjs with typescript?

Typescript’s goal is simply be Javascript + Types. There are many use cases where one might want to use the excellent typechecker but not really have any emit stage. Projects already written in javascript work with allowJS. Typescript already supports parsing types from jsdoc comments.


1 Answers

If nobody finds an "official" way to do this, you could always put the comments in a section of code that you know will be erased from the emitted JavaScript. It's definitely a hack, but depending on your use case it could work for you:

interface TSOnlyComment {
  // this comment will definitely not appear in the JS
}

So that's a dummy empty interface named TSOnlyComment. You can reuse that interface to make multiple comments, since TypeScript will interpret that as interface merging:

interface TSOnlyComment {
  // This only appears in TS, not JS
}

const javaScriptAwesomenessFactor = 1e9; // JS rules!

interface TSOnlyComment {
  // Just kidding, TS is better than JS.
}

// let's print out how awesome JS is
console.log(javaScriptAwesomenessFactor);

interface TSOnlyComment {
  // Ha ha, we are laughing at JS behind its back.
}

That should emit JS something like (depending on your --target):

var javaScriptAwesomenessFactor = 1e9; // JS rules!
// let's print out how awesome JS is
console.log(javaScriptAwesomenessFactor);

So, maybe that works for you? Good luck!

like image 61
jcalz Avatar answered Sep 28 '22 00:09

jcalz