How to document a function returned by a function using JSDoc
Asked Answered
C

4

58

I am using JSDoc for parameter documentation.

It is clear how to document the parameter types for many_prompts, but what is the right way to document the function it returns?

/**
 * @param {Number} - number of times to prompt
 * @return {Function(prompt{Number})} - the returned function
 */
function many_prompts(count) {
  return function(prompt) {
    for(var i=0; i < count; i++) alert(prompt);
  }
}


//Example of use:
var y  =many_prompts(3);
y('Hello World');
Caundra answered 3/5, 2015 at 9:3 Comment(0)
C
24

The way I prefer:

/**
 * @param {number} count - number of times to prompt
 * @returns { (prompt:string) => void } - the returned function
 */
  manyPrompts(count) {
      /**
       * My inner function
       *
       * @param {object} prompt Some parameter
       */
      const inner = function(prompt) {
        for (let i=0; i < count; i++) {
          alert(prompt);
        };
      };
      return inner;
  }
Caundra answered 22/3, 2020 at 16:5 Comment(6)
Is there any documentation for this?Lordan
Yeah, is this standard or non standard?Wohlen
Works in VSCodeFahlband
promt seems misspelt.Germinal
also works when I use tsc to generate .d.ts filesZipangu
Thanks. Edited. promt fixed to promptCaundra
S
26

This seems to be working for me.

 /**
 * @param {Number} count - number of times to prompt
 * @return {function(): void} - the returned function
 */
  manyPrompts(count) {
      /**
       * My inner function
       *
       * @param {object} prompt Some parameter
       */
      const inner = function(prompt) {
        for (let i=0; i < count; i++) {
          alert(prompt);
        };
      };
      return inner;
  }
Selfimportant answered 27/6, 2018 at 14:35 Comment(3)
You also don't need the parentheses: @return {function: void} worksHoyos
This seems to work in VS Code, but doesn't pick up on the inner function documentation. :(Payton
I was able to use this one with parameters inside the parentheses: @returns {(function([string], string, *, *): void)}, doesn't seem to be possible to name them, though, but the IDEA, at least, parsing the types of the returned function.Cia
R
25

You can document the inner function and then reference it like so

/**
 * @param {Number} - number of times to prompt
 * @return {many_prompts~inner} - the returned function
 */
function many_prompts(count){
  /**
   * My inner function
   *
   * @param {object} prompt Some parameter
   */
  var inner = function(prompt){
    for(var i=0;i<count;i++) alert(prompt)
  };
  return inner;
}
Reptant answered 22/5, 2015 at 10:10 Comment(7)
Is there a way to do this for anonymous inner functions?Boise
For JSDoc you would need some form of reference, I guess it depends on how the anonymous function is used. Do you have an example?Reptant
Is this documented anywhere officially? Can't find it.Label
Not in full depth I'm afraid. Did you try tagging your inner function with @function many_prompts~innerReptant
Is there a way to do this if you're using arrow functions? For instance: many_prompts = count => prompt => ...Improbity
I have never tried, theoretically it should work as it is semantically the same but the question is if the available parsers do support this already.Reptant
This doesn't seem to be recognized by VS Code.Payton
C
24

The way I prefer:

/**
 * @param {number} count - number of times to prompt
 * @returns { (prompt:string) => void } - the returned function
 */
  manyPrompts(count) {
      /**
       * My inner function
       *
       * @param {object} prompt Some parameter
       */
      const inner = function(prompt) {
        for (let i=0; i < count; i++) {
          alert(prompt);
        };
      };
      return inner;
  }
Caundra answered 22/3, 2020 at 16:5 Comment(6)
Is there any documentation for this?Lordan
Yeah, is this standard or non standard?Wohlen
Works in VSCodeFahlband
promt seems misspelt.Germinal
also works when I use tsc to generate .d.ts filesZipangu
Thanks. Edited. promt fixed to promptCaundra
G
0

This is my solution for this. I'm not describing a return in the first function and also document the inner function, which results in getting the documentation from the inner function.

/**
 * Function to create a Function with multiple prompt messages
 * @function
 * @param {Number} count - number of times to prompt
 */
function many_prompts(count) {
  /** 
   * To prompt a value multiple times
   * @function
   * @param {String} prompt - parameter to prompt
   * @return {void} prompt the input parameter
   */
  return function(prompt) {
    for(var i=0; i < count; i++) alert(prompt);
  }
}


//Example of use:
var y  = many_prompts(3);
y('Hello World');

Which is then shown e.g. in vscode like this...

For the outer function: description of the outer function in vscode

For the inner function:

description of the inner function in vscode


You can also add an additional description when assigning the function, to describe the difference

/** 
 * Function to prompt a value 3 times
 * @function
 * @param {Number} prompt - parameter to prompt
 * @return {void} prompt the input parameter
 */
const y = many_prompts(3);
y('Hello World');
Galasyn answered 22/1, 2023 at 18:10 Comment(2)
> @return {Function} It never returns Function. Your example returns void.Cia
@Cia Thanks, that was wrong! I changed it to void.Galasyn

© 2022 - 2024 — McMap. All rights reserved.