thatDude767
thatDude767

Reputation: 88

Make JSDoc comment for function which takes another function as parameter

Imagine a function which accepts an "onUpdate" function as a parameter:

 /**
 * @param {function} onUpdate
 * @returns {null}
 */
static async init(onUpdate) {
    ...
    onUpdate(true);
};

onUpdate takes a boolean as an argument. I want to add that arguments to @param {function} onUpdate.

Is there a way?

Some magnificent devs managed to do it on these addEventListener functions:

Upvotes: 0

Views: 446

Answers (2)

Denis S Dujota
Denis S Dujota

Reputation: 611

If you know the types ahead of time you can destructure it at the definition inline allowing for more complex parameters typing on your callback, for example arrays, objects etc.

/**
 * @callback myCallback
 * @param { { param1: number, param2: string } } - Some human friendly explanation
 *
 *
 * @returns {void}
 */

/**
 *
 * @param {myCallback} cb - The function does some cool stuff
 * @param {string} somethingElse - can still define more params for fn
 * @param {number} anotherThng - the same way as before
 *
 *
 * @returns { void } - can be any return you want to type out
 */

Upvotes: 0

dikobrazik
dikobrazik

Reputation: 66

I think in screenshot you see typescript hint. But you can do something like this:

/**
 * This callback is displayed as a global member.
 * @callback someCallback
 * @param {number} param1
 * @param {number} param2
 * @returns {number}
 */

/**
 * 
 * @param {someCallback} cb 
 */

function someFunction(cb) {

}

And see this hint

Upvotes: 1

Related Questions