Document overloaded function in JSDoc

我怕爱的太早我们不能终老 提交于 2019-12-02 10:44:22

问题


I have an overloaded toggle function and want to document the behaviors w/ 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. 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 });
}

回答1:


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.




回答2:


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

(this works in standard JS, as well as TS)



来源:https://stackoverflow.com/questions/49523358/document-overloaded-function-in-jsdoc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!