本文介绍了jsdoc和vscode:记录作为参数传递给另一个函数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将输入参数记录到javascript中的函数中,但是我不知道如何在jsdoc中做到这一点.

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.

我看过jsdoc文档,该文档建议使用@callback注释是必需的,但是Visual Studio Code(vscode)并未按照屏幕快照突出显示它.

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.

location参数的智能感知表明它是any类型而不是locator类型(具有id参数的函数返回Location).

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).

示例代码显示了一个函数,该函数调用作为参数传递的函数:

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);

这是我在做什么吗,vsdoc不支持用例,还是vscode不支持?

Is this a problem with what I'm doing, vsdoc not supporting the use case, or vscode not supporting it?

推荐答案

Vscode的IntelliSense不支持@callback.它在这里被跟踪: https://github.com/Microsoft/TypeScript/issues/7515.

Vscode's IntelliSense doesn't support @callback. It's being tracked here: https://github.com/Microsoft/TypeScript/issues/7515.

作为一种方便的解决方法,您可以使用 @typedef :

As a convenient workaround you can use @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和vscode:记录作为参数传递给另一个函数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 03:20