jsdoc和vscode:将作为参数传递给另一个函数的函数记录下来

问题描述 投票:2回答:2

我正在尝试将输入参数记录到javascript中的函数,但我无法弄清楚如何在jsdoc中执行此操作。

我查看了jsdoc文档,该文档表明使用@callback注释是必需的,但Visual Studio Code(vscode)不会根据屏幕截图突出显示它。

location参数的intellisense显示它的类型为any而不是locator类型(id参数返回Location的函数)。

shows the locater parameter not being type hinted

显示调用作为参数传递的函数的函数的示例代码:

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不支持它?

javascript node.js visual-studio-code jsdoc
2个回答
3
投票

编辑: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);

enter image description here


1
投票

根据JSDoc本身看起来你正确使用它。但是,看起来Visual Studio可能只支持JSDoc的有限子集,其中不包括@callbackhttps://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的东西,它支持这种语法。

© www.soinside.com 2019 - 2024. All rights reserved.