In JSDoc, you can use the @type
tag to override the type of a class field. This allows you to provide more specific type information or clarify the type when the inferred type might not be accurate. To override the type of a class field, you add the @type
tag immediately above the field declaration.
Here's an example of how to use the @type
tag to override the type of a class field:
javascript/**
* Class representing a Person.
*/
class Person {
/**
* Create a Person.
* @param {string} name - The name of the person.
* @param {number} age - The age of the person.
*/
constructor(name, age) {
/**
* The name of the person.
* @type {string}
*/
this.name = name;
/**
* The age of the person.
* @type {number}
*/
this.age = age;
}
/**
* Get the name of the person.
* @returns {string} The name of the person.
*/
getName() {
return this.name;
}
/**
* Get the age of the person.
* @returns {number} The age of the person.
*/
getAge() {
return this.age;
}
}
In this example, we have a Person
class with two fields: name
and age
. The @type
tags are used to specify that the name
field is of type string
, and the age
field is of type number
.
By using the @type
tag, you can provide additional information to the JSDoc tooling and make your code documentation more accurate and helpful for consumers of your class. Note that JSDoc is a convention for documenting JavaScript code, and the type information specified with @type
is not enforced by the language itself. It is primarily used for documentation purposes and tooling support.