简体   繁体   English

如何在webstorm中使用JSDoc @param回调处理程序的参数?

[英]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. 我试图在处理程序的参数上使用JSDoc提示,但是它不起作用。 I have tried with @type and @param and it does not work. 我已经尝试使用@type和@param,但它不起作用。 The official JSDoc did not contain any helpful information regarding this problem. 官方的JSDoc没有包含有关此问题的任何有用信息。

This does not work: 这不起作用:

    socket.on( "data",
    /**
     * @param request {Object}
     * @param request.code {Number}
     * @param request.id {Number}
     * @param request.sr {String}
     */
    function( request )
    {});

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: 这是针对jsdoc3的,但我认为是相同的:

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

You can use complex "typedef" and "property" tags. 您可以使用复杂的“ typedef”和“ property”标签。 Documented in: http://usejsdoc.org/tags-typedef.html However, "~" char seems to prevent WebIde to link type annotations. 文档记录在: http ://usejsdoc.org/tags-typedef.html但是,“〜” char似乎阻止WebId链接类型注释。 (Just use plain typedef MyType annotation without tilde and it works) (只需使用不带波浪号的普通typedef MyType注释,它就可以工作)

BTW, for Google Closure way: 顺便说一句,对于Google关闭方式:

/** @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. 此jsdoc批注特别适用于Google Closure,但仅出于提示的目的,可以在不使用Closure的情况下使用。 (should work since August 2012: http://blog.jetbrains.com/webide/2012/08/closure-syntax/ ) (自2012年8月起应该可以使用: http : //blog.jetbrains.com/webide/2012/08/closure-syntax/

This is vague question, so here's my guess: 这是一个模糊的问题,所以这是我的猜测:

socket.on( "data",
/**
 * @param request {Object}
 * @param request.code {Number}
 * @param request.id {Number}
 * @param request.sr {String}
 */
function( request )
  typeof request.id == 'number';
  console.log(request.sr); // will print out the string
{});

The comment just describes which data / keys should be expected in the request object. 注释仅描述了请求对象中应该包含哪些数据/密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM