[英]jsdoc and vscode: Documenting a function passed as an argument to another function
I'm trying to document the input parameters to a function in javascript, but I can't work out how to do it in jsdoc. 我正在尝试将输入参数记录到javascript中的函数中,但是我不知道如何在jsdoc中做到这一点。
I've looked at the jsdoc documentation which suggests that using the @callback
comment is what's required, but Visual Studio Code (vscode) doesn't highlight it as per the screenshot. 我看过jsdoc文档,该文档建议使用@callback
注释是必需的,但是Visual Studio Code(vscode)并未按照屏幕截图突出显示它。
The intellisense for the location
parameter shows that it's type any
rather than of type locator
(a function with a parameter of id
which returns a Location
). 对location
参数的智能感知表明,它的类型为any
而不是locator
类型(具有id
参数的函数返回Location
)。
Example code which shows a function calling a function passed as a parameter: 示例代码显示了一个函数,该函数调用作为参数传递的函数:
class Location {
constructor(position, count) {
this.position = position;
this.count = count;
}
}
const items = {
'USB Cable': new Location('Desk Drawer', 123),
Keyboard: new Location('Desk Surface', 1),
};
/**
* A locater.
* @param {string} id
* @returns {Location}
*/
const locaterA = id => items[id];
/**
* Finds the item by its unique id.
* @callback locater
* @param {string} id
* @returns {Location}
*/
/**
* Attempt to find the item with the given locater.
* @param {string} id
* @param {locater} locater
*/
const locate = (id, locater) => locater(id);
const result = locate('USB Cable', locaterA);
console.log(result);
Is this a problem with what I'm doing, vsdoc not supporting the use case, or vscode not supporting it? 这是我正在做什么的问题,vsdoc不支持用例,还是vscode不支持?
Edit : vscode seems to be supporting
@callback
starting from TypeScript 2.9 编辑 :vscode似乎从TypeScript 2.9开始支持@callback
Vscode's IntelliSense doesn't support @callback
. Vscode的IntelliSense不支持@callback
。 It's being tracked here: https://github.com/Microsoft/TypeScript/issues/7515 . 它在这里被跟踪: https : //github.com/Microsoft/TypeScript/issues/7515 。
As a convenient workaround you can use @typedef : 作为一种方便的解决方法,您可以使用@typedef :
/**
* Finds the item by its unique id.
* @typedef {function(string): Location} Locater
*/
/**
* Attempt to find the item with the given locater.
* @param {string} id
* @param {Locater} locater
*/
const locate = (id, locater) => locater(id);
It looks like you're using it correctly, per JSDoc itself. 根据JSDoc本身,您似乎正在正确使用它。 However, it looks like Visual Studio may only support a limited subset of JSDoc, which doesn't include @callback
: https://msdn.microsoft.com/en-us/library/mt162307.aspx 但是,似乎Visual Studio可能仅支持JSDoc的有限子集,其中不包括@callback
: https : @callback
I don't have Visual Studio handy, but you might try the Google Closure style, which is to do it like this: 我没有使用Visual Studio,但是您可以尝试使用Google Closure样式,方法如下:
@param { function(string) : number } locator
This says that it's a function that takes a string, and returns a number. 这表示它是一个接受字符串并返回数字的函数。
You can find that documentation here: https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler (search for "Function Return Type" to jump to the relevant section). 您可以在以下位置找到该文档: https : //github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler (搜索“函数返回类型”以跳至相关部分)。
I've noticed with JetBrains stuff at least, that it supports that syntax. 我至少注意到JetBrains的东西,它支持该语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.