7.8 Method and function comments
In methods and named functions, parameter and return types must be documented,except in the case of same-signature @override
s, where all types are omitted.The this
type should be documented when necessary. Return type may be omittedif the function has no non-empty return
statements.
Method, parameter, and return descriptions (but not types) may be omitted ifthey are obvious from the rest of the method’s JSDoc or from its signature.
Method descriptions begin with a verb phrase that describes what the methoddoes. This phrase is not an imperative sentence, but instead is written in thethird person, as if there is an implied This method …
before it.
If a method overrides a superclass method, it must include an @override
annotation. Overridden methods inherit all JSDoc annotations from the superclass method (including visibility annotations) and they should be omitted inthe overridden method. However, if any type is refined in type annotations, all@param
and @return
annotations must be specified explicitly.
/** A class that does something. */
class SomeClass extends SomeBaseClass {
/**
* Operates on an instance of MyClass and returns something.
* @param {!MyClass} obj An object that for some reason needs detailed
* explanation that spans multiple lines.
* @param {!OtherClass} obviousOtherClass
* @return {boolean} Whether something occurred.
*/
someMethod(obj, obviousOtherClass) { ... }
/** @override */
overriddenMethod(param) { ... }
}
/**
* Demonstrates how top-level functions follow the same rules. This one
* makes an array.
* @param {TYPE} arg
* @return {!Array<TYPE>}
* @template TYPE
*/
function makeArray(arg) { ... }
If you only need to document the param and return types of a function, you mayoptionally use inline JSDocs in the function's signature. These inline JSDocsspecify the return and param types without tags.
function /** string */ foo(/** number */ arg) {...}
If you need descriptions or tags, use a single JSDoc comment above the method.For example, methods which return values need a @return
tag.
class MyClass {
/**
* @param {number} arg
* @return {string}
*/
bar(arg) {...}
}
// Illegal inline JSDocs.
class MyClass {
/** @return {string} */ foo() {...}
}
/** Function description. */ bar() {...}
In anonymous functions annotations are generally optional. If the automatic typeinference is insufficient or explicit annotation improves readability, thenannotate param and return types like this:
promise.then(
/** @return {string} */
(/** !Array<string> */ items) => {
doSomethingWith(items);
return items[0];
});
For function type expressions, see ??.