简体   繁体   English

JSDoc 中的文档重载函数

[英]Document overloaded function in JSDoc

I have an overloaded toggle function and want to document the behaviors w/ JSDoc.我有一个重载的切换功能,想用 JSDoc 记录这些行为。

If the value is defined the window state is set to the boolean value of the truthy parameter, if undefined the window state toggles.如果该值已定义,则窗口状态设置为truthy 参数的布尔值,如果未定义,则窗口状态切换。 I'm looking for something like this.我正在寻找这样的东西。

/**
 * 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 });
}

Taken from 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) { // ... }

Note, however, that constructor overloads are not grouped together.但是请注意,构造函数重载没有组合在一起。 Each overload still receives the full member list, such that part of the documentation becomes redundant.每个重载仍然接收完整的成员列表,因此部分文档变得多余。 Might be fixable in the template, however.但是,可能可以在模板中修复。

Since the previous answer didn't work for me.由于之前的答案对我不起作用。 Try:尝试:

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

Please give credit for this answer to ExE-Boss on GitHub, found here: https://github.com/microsoft/TypeScript/issues/25590#issuecomment-480022039请将此答案归功于GitHub 上的ExE-Boss ,可在此处找到: https : //github.com/microsoft/TypeScript/issues/25590#issuecomment-480022039

(this works in standard JS, as well as TS) (这适用于标准 JS 以及 TS)

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

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