Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I JSDoc @param in webstorm a callback handler's parameters?

I am trying to use JSDoc hinting on the parameter of a handler but it doesn't work. I have tried with @type and @param and it does not work. The official JSDoc did not contain any helpful information regarding this problem.

This does not work:

    socket.on( "data",
    /**
     * @param request {Object}
     * @param request.code {Number}
     * @param request.id {Number}
     * @param request.sr {String}
     */
    function( request )
    {});
like image 634
Discipol Avatar asked May 02 '13 23:05

Discipol


People also ask

What is the JSDoc keyword to specify an argument to a function?

The @param tag provides the name, type, and description of a function parameter. The @param tag requires you to specify the name of the parameter you are documenting.

What are JSDoc comments?

JSDoc comments are used for documentation lookup with Ctrl+Q in JavaScript and TypeScript, see JavaScript documentation look-up and TypeScript documentation look-up, as well as for type annotations and method return type hints in chained methods.

What is Param JavaScript?

The param() method creates a serialized representation of an array or an object. The serialized values can be used in the URL query string when making an AJAX request.


2 Answers

You can use complex "typedef" and "property" tags. Documented in: http://usejsdoc.org/tags-typedef.html However, "~" char seems to prevent WebIde to link type annotations. (Just use plain typedef MyType annotation without tilde and it works)

BTW, for Google Closure way:

/** @typedef {{code: Number, id: Number, str: String}} **/
SocketRequest;

socket.on("data", handler);

/**
 * @param {SocketRequest} req
 */
function handler(req) {
    //req will be hinted here
}

This jsdoc annotation is especially for Google Closure, but can be used without Closure just for the sake of hinting. (should work since August 2012: http://blog.jetbrains.com/webide/2012/08/closure-syntax/)

like image 143
CKK Avatar answered Oct 01 '22 22:10

CKK


I think you swapped the type and name of the objects, perhaps swapping them could help?

This is for jsdoc3, but I think it is the same:

http://usejsdoc.org/tags-param.html

like image 42
madsdyd Avatar answered Oct 01 '22 22:10

madsdyd