我正在尝试将输入参数记录到javascript中的函数,但我无法弄清楚如何在jsdoc中执行此操作。
我查看了jsdoc文档,该文档表明使用@callback
注释是必需的,但Visual Studio Code(vscode)不会根据屏幕截图突出显示它。
location
参数的intellisense显示它的类型为any
而不是locator
类型(id
参数返回Location
的函数)。
显示调用作为参数传递的函数的函数的示例代码:
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);
这是我正在做的问题,vsdoc不支持用例,还是vscode不支持它?
编辑:vscode似乎从TypeScript 2.9开始支持
@callback
Vscode的IntelliSense不支持@callback
。它在这里被跟踪:https://github.com/Microsoft/TypeScript/issues/7515。
作为一种方便的解决方法,您可以使用@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);
根据JSDoc本身看起来你正确使用它。但是,看起来Visual Studio可能只支持JSDoc的有限子集,其中不包括@callback
:https://msdn.microsoft.com/en-us/library/mt162307.aspx
我没有Visual Studio方便,但您可以尝试使用Google Closure样式,这样做是这样的:
@param { function(string) : number } locator
这表示它是一个带字符串的函数,并返回一个数字。
您可以在此处找到该文档:https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler(搜索“函数返回类型”以跳转到相关部分)。
我至少注意到JetBrains的东西,它支持这种语法。