简体   繁体   English

如何使用JSDoc记录不一致的退货?

[英]How to document inconsistent returns with JSDoc?

Is it possible to add @return JSDoc annotation for a function with inconsistent returns? 是否可以为@return不一致的函数添加@return JSDoc注释?

/**
* @param {Number} a
* @param {Number} b
* @return {Number}
* */
function sum (a, b) {
  if (a < b) return false; //here we have a Boolean
  return a + b; //and here a Number
}

Note: 注意:

  1. I already know that this is bad . 我已经知道这很糟糕
  2. I won't do it in my code. 不会在我的代码中这样做
  3. Just interested in how everyone handle this issue (in other ways than refactoring). 只对每个人如何处理此问题感兴趣 (通过重构以外的其他方式)。

There's an example right there in the JSDoc @returns documentation : JSDoc @returns文档中有一个示例:

The return value can have different types 返回值可以有不同的类型

 /** * Returns the sum of a and b * @param {Number} a * @param {Number} b * @param {Boolean} retArr If set to true, the function will return an array * @returns {Number|Array} Sum of a and b or an array that contains a, b and the sum of a and b. */ function sum(a, b, retArr) { if (retArr) { return [a, b, a + b]; } return a + b; } 

Eg, use | 例如,使用| , as you do other times the type may vary. ,就像您其他时候一样,类型可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM