jsdoc和vscode:logging作为parameter passing给另一个函数的函数

我试图在JavaScript中logginginput参数的函数,但我不能解决如何在jsdoc中做到这一点。

我查看了jsdoc文档,它build议使用@callback注释是必需的,但Visual Studio代码(vscode)不会像截图一样突出显示它。

location参数的intellisense显示它是typesany而不是typeslocator (带有返回Locationid参数的函数)。

显示定位器参数不是被键入的

显示调用函数的函数的示例代码作为parameter passing:

 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 : @callback

我没有Visual Studio的方便,但你可以尝试谷歌闭合风格,这样做是这样的:

 @param { function(string) : number } locator 

这就是说这是一个接受一个string并返回一个数字的函数。

你可以在这里find这个文档: https : //github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler (search“函数返回types”跳转到相关章节)。

我注意到JetBrains的东西至less,它支持这种语法。