JSDoc 中的文档重载函数

Document overloaded function in JSDoc

我有一个重载的 toggle 函数,想用 JSDoc 记录行为。

如果值已定义,window 状态将设置为 truthy 参数的布尔值,如果未定义,window 状态将切换。我正在寻找这样的东西。

/**
 * Set the map window in mobile
 * @param {undefined|*} on - toggle or set the window state
 *  - {undefined} toggles window state
 *  - {*} set window state
 */
toggleWindow(on) {
  if (on === undefined) {
    on = !this.state.window;
  }
  this.setState({ mapWindow: !!on });
}

取自here:

You need to nestle the start and end of each comment together like so:

/**
 * DateRange class to store ranges and query dates.
 *
 * @constructor
 * @param {(Moment|Date)} start Start of interval
 * @param {(Moment|Date)} end End of interval
 *//**
 * DateRange class to store ranges and query dates.
 *
 * @constructor
 * @param {!Array} range Array containing start and end dates.
 *//**
 * DateRange class to store ranges and query dates.
 *
 * @constructor
 * @param {!String} range String formatted as an IS0 8601 time interval
 */
function DateRange(start, end) {
  // ...
}

但是请注意,构造函数重载不会组合在一起。每个重载仍然接收完整的成员列表,这样部分文档就变得多余了。但是,可能可以在模板中修复。

因为之前的答案对我不起作用。尝试:

/** @type {((param: string) => boolean) & ((param: string, overloadedParam: string) => string))} */
const func = (param, overloadedParam) => { … }

请将此答案归功于 GitHub 上的 ExE-Boss,可在此处找到: https://github.com/microsoft/TypeScript/issues/25590#issuecomment-480022039

(这适用于标准 JS 和 TS)