简体   繁体   English

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

[英]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的有限子集,其中不包括@callbackhttps : @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.

相关问题 在JSDoc中将对象数组记录为回调函数的参数 - Documenting an array of objects as a parameter of callback function in JSDoc VS代码中的JSDoc,用于记录具有模块类型的函数 - JSDoc in VS code for documenting function with module's type 如何确定一个函数的参数是否作为参数传递给另一个函数 - how to determine if the argument of a function passed into another function as an argument is undefined or not 如何记录参数对象的函数定义 - How to documenting an argument object's function definition JSdoc: function 参数中的节点元素类型 - JSdoc: node element type in function argument 将 function 作为参数传递给另一个 function 并将其应用于传入的元素 - Passing in a function as an argument to another function and apply it to the passed in element 在jQuery对象上调用作为参数传递给另一个函数的函数 - Call a function, passed to another function as an argument, on a jQuery object 将参数传递给函数作为参数传递给jQuery函数 - pass argument to function passed as argument to jQuery function 将数组(作为函数参数传递)的元素复制到JavaScript中的另一个本地数组中 - Copy array (passed as a function argument) elements into another local array in JavaScript JSDoc:如何记录返回传递的构造函数实例的函数? - JSDoc: how to document a function that returns an instance of a passed constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM