Does Typescript's /** @class */ have a purpose

2020-08-20 07:35发布

I am learning typescript and I noticed that the compiled javascript has a comment for every class that looks like this: /** @class */

Example:

var Student = /** @class */ (function () {
    function Student(firstName, middleInitial, lastName) {
        this.firstName = firstName;
        this.middleInitial = middleInitial;
        this.lastName = lastName;
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
    return Student;
}());

My question is does this comment have any functional value or is it just syntactical sugar? Also, If it does have a function, what is the function?

3条回答
疯言疯语
2楼-- · 2020-08-20 07:40

This is emitted so that minifiers can remove unused classes. To minifiers, the class setup looks like something with potentially important side effects, so this comment indicates to them that the code block can be safely removed if the rest of the code doesn't refer to it.

查看更多
仙女界的扛把子
3楼-- · 2020-08-20 07:53

It's outputting a JSDoc comment in case you want to document your code: http://usejsdoc.org/tags-class.html.

In this case, the tag indicates to the documentation generator that the following function is a constructor.

It has no functional value, nor is it syntactic sugar. It's there in case you decide to generate documentation. Nothing more.

查看更多
趁早两清
4楼-- · 2020-08-20 08:01

My question is does this comment have any functional value or is it just syntactical sugar?

Neither, because it's a comment, not its own syntactical construct.

The comment is simply JSDoc. @Amy's answer has the links for that.

查看更多
登录 后发表回答