jsdoc和vscode:记录一个函数作为参数传递给另一个函数
我试图在javascript中记录输入参数到一个函数,但我无法解决如何在jsdoc中执行它。jsdoc和vscode:记录一个函数作为参数传递给另一个函数
我看了一下jsdoc文档,它建议使用@callback
评论是必需的,但Visual Studio代码(vscode)不会按照屏幕截图来突出显示它。
用于location
参数的智能感知表明,它的类型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的智能感知不支持@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方便,但你可以尝试谷歌闭合风格,这是做它是这样的:
@param { function(string) : number } locator
这就是说,它是一个函数,需要一个字符串,并返回一个数字。
你可以在这里找到该文档:https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler(搜索“函数返回类型”跳转到相关章节)。
我注意到JetBrains至少有东西,它支持这种语法。