7

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 });
}
3
  • Unrelated to your actual question, but this seems like it would be clearer if you just left toggleWindow as a function taking no parameters and then moved the other functionality into something like setWindow.
    – Blender
    Mar 27, 2018 at 22:29
  • default value? usejsdoc.org/… Mar 27, 2018 at 22:30
  • @epascarello on=!this.state.window isn't a bad idea since default parameters are re-evaluated at "call time" Even though it doesn't directly answer the question, such an answer should earn you some upvotes.
    – Shanimal
    Nov 15, 2019 at 18:27

2 Answers 2

4

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.

1
  • I think this is only valid for constructors, not for functions. This answer worked for me. I had to add a type ignore as well at the function definition (but not at the use points). Feb 25 at 18:29
0

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)

1
  • 1
    It doesn't "work" in standard JS in that vanilla JSDoc tooling will not understand the arrow-function syntax you're using, and command line jsdoc will fail to parse it and generate an error. What you wrote is TypeScript and will only work in TS-compatible doc parsers.
    – Coderer
    Apr 14, 2022 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.