Why is TypeScript Adding Class Comments to my Code

I noticed all my TypeScript compiled files had been modified after a recent change. I hadn't changed anything in them so this gave me some concern. What I found was TypeScript added a /** @class */ comment to the modified files. This made no sense to me, so I did some digging.

What is the Reason For /** @class */ in TypeScript Compiled Files

It appears the /** @class */ comment has a purpose. This comment helps with tree-shaking during code minification. It informs the minifier that this code block can be safely removed if no other code is referring to it. This ends up being a huge win in reducing code size.

In short, make sure to accept these changes into your repository. Then check to see if it did indeed shrink your file sizes at all.

If for some reason you want to get rid of the comment, you can compile your TypeScript with --removeComments.

One Last Thing...

If you have a question or see a mistake, please comment below.

If you found this post helpful, please share it with others. It's the best thanks I can ask for & it gives me momentum to keep writing!

Matt Ferderer
Software Developer focused on making great user experiences. I enjoy learning, sharing & helping others make amazing things.
Let's Connect