16

I'm trying to document my code with JSDoc (EcmaScript 2015, WebStorm 12 Build 144.3357.8).

I have an arrow function which I want to document its parameters. This two examples work (I get auto-completion):

/** @param {Number} num1*/
var a = num1 => num1 * num1;
//------------------------------
/** @param {Number} num1*/
var a = num1 => {
    return num1 * num1;
};

But when I want to document an arrow function in forEach function, for example, the auto-completion isn't working (all of the below):

/** @param {Number} num1*/
[].forEach(num1 => {
    return num1 * num1;
});
//------------------------------
/** @param {Number} num1*/
[].forEach(num1 => num1 * num1);
//------------------------------
[].forEach(/** @param {Number} num1*/num1 => num1 * num1);
//------------------------------
[].forEach(/** @param {Number} num1*/num1 => {
    return num1 * num1;
});

Has anyone managed to get this work?

2 Answers 2

16

Starting from the next EAP build, WebStorm will understand this:

[].forEach(/**Number*/num1 => {
    return num1 * num1;
});

Please look at WEB-19280 for details.

2
  • 3
    I came across this question when looking for vscode solution that's how it's done in vscode [].forEach(/** @param {Number} num1*/ num1 => { .... } Commented Jan 20, 2020 at 6:56
  • For VSC you can cast the array to the type you want to get autocompletion in the anonymous function: (/**@type {Array<number>}*/([])).forEach(num1 => num1 * num1)
    – Jonathan
    Commented Oct 2, 2023 at 7:59
5

You can use this format:

/**
 * @param {(prevState: ActionState) => ActionState} setActionState
 */
function f(setActionState) {
  // ...
}

Or:

/**
 * @param {function(prevState: ActionState): ActionState} setActionState
 */
function f(setActionState) {
  // ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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