98

在 JsDoc 中是否有指定的方法来声明返回 void 的方法或函数?目前我认为void是默认返回值,其他返回值必须具体提供:

/**
 * @return {Integer} The identifier for ...
 */
4

5 回答 5

115

闭包编译器

根据 Google 的 Closure Compiler 的文档,如果没有返回任何内容,则应省略 @return 注释。

如果没有返回值,请不要使用@return 标记。

来源: https ://developers.google.com/closure/compiler/docs/js-for-compiler#tags

jsdoc-工具包

然而,进一步的文档还指出 returnType 和 returnDescription 是可选参数。

returnType - 可选:返回值的类型。

returnDescription - 可选:任何附加描述。

来源: https ://code.google.com/p/jsdoc-toolkit/wiki/TagReturns

概括

您可以省略返回注释,也可以在不包含任何参数的情况下包含它。

于 2013-12-18T05:04:35.460 回答
106

我不相信你必须从 JsDoc 中的一组类型中进行选择......你可以使用任何你想要的类型名称(花括号表示它是一种类型),所以你可以简单地做:

@return {void}

虽然,这对于 JavaScript 可能更正确:

@return {undefined}
于 2011-01-21T13:20:56.213 回答
24

查看他们使用的 ESlint 文档 @returns {void}

来源:http ://eslint.org/docs/rules/valid-jsdoc

由于我需要@returns为每个函数提供一个通过测试以便为某些项目推送代码,这在我的情况下是必需的。

于 2017-08-02T02:29:24.827 回答
1

If you need to say out loud that nothing is returned, you can say that in the free-form description. This is useful to clarify situations where a user might expect something to be returned. Of course proper naming of the function and the parameters should alone make the expected return type apparent, but it might not always be possible.

/**
 * This is a funny function. Returns nothing.
 * @param {string} a joke.
 */
var funny = function (joke) {
  console.log(joke);
};
于 2015-03-10T08:24:21.243 回答
0

我发现这比省略@returns

@returns {} Nothing is returned.

甚至

@returns {} Nothing is returned because this is a broadcast receiver.

只是一个想法。

于 2021-11-20T22:22:51.690 回答