Search code examples
javascriptphpstormanonymous-functionjsdoc

How to describe parameters of passed anonymous function?


I want to describe curVal with JSDoc?

I tried @param just before arrays.every() and inside anonymous callback, but it didn't help for PhpStorm to resolve methods.

/**
 * @param {Array} curVal
 */
arrays.every(function (curVal) {
    /**
     * @param {Array} curVal
     */     
    curVal.???
});

I need it for convinience, I just want to easily access in IDE's autocompletion Array.prototype methods on object curVal


Solution

  • I don't know how smart PhpStorm is (the docs say it recognizes Closure Compiler tags and type annotations), but I can think of two possible solutions.

    First is to tell it directly the type of the function param:

    arrays.every(/** @param {Array} curVal */ function (curVal) {
      // ...
    });
    

    Or (Closure Compiler inline style):

    arrays.every(function (/** Array */ curVal) {
      // ...
    });
    

    Second, and this will only work if PhpStorm is smart enough to know how Array.prototype.every's callback gets its arguments, is to make sure it knows arrays is an array of arrays:

    /** @type {Array.<Array>} */
    var arrays = getArrays();
    

    Or:

    var arrays = /** Array.<Array> */ getArrays();