5

I just cannot figure out how to correctly document callback using @param so that Visual Studio 2017 intellisense will understand it.

For example:

/**
 * @param {string} file absolute path
 * @param {Function} callback called when done
*/
function loadFile(path, callback) {
    /// code
}

The callback accepts Error and string as arguments (Node.js style), how to document it?

2 Answers 2

13

This pattern works:

/**
 * @param {string} file absolute path
 * @param {function(Error, string):void} callback called when done
*/
function loadFile(path, callback) {
    /// code
}

void here stands for no return value, can be replaced with callback return value (eg.: {function(value):boolean} for a predicate).

How to document parameter names I do not know.

Sign up to request clarification or add additional context in comments.

6 Comments

OMG you are my hero, @tomáš-zato . I tried using the official JSDoc notation for callbacks and nothing worked until I saw this. I wish I could upvote this answer multiple times.
Bonus if you can figure out how to annotate the parameters with variable names and descriptions!
@KrisOye I wish. I tried all kinds of combinations without success.
Is there a way to specify the name of the parameters too along with the types?
@tonix Not that I know of. You could probably define dummy function somewhere and refer to it as it were a type.
|
5

To annotate the callback parameters with types and names, do this:

/**
 * @param {string} path - absolute file path
 * @param {(error: string, namedParameter: type)} callback - callback called when done
*/
function loadFile(path, callback) {
  /// code
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.