如何在JSDOC(或Typescript?)中记录函数自定义类型并引用它们,以便VSCODE IntelliSense起作用

发布于 2025-01-22 09:10:25 字数 1789 浏览 2 评论 0原文

我正在尝试将自定义函数类型作为对象的一部分进行记录,任何帮助都将不胜感激:

问题的上下文

是一个简单的对象声明,其中有某些函数属性(AddCoorcorion,AddCoordionAne,AddCoordinateTwo),我们将在3个展览中使用它,以及为什么这些都没有工作。

/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */

/**
 * @typedef {Object} Shape
 * @property {Point} startCoordinate - the starting coordinate
 * @property {Point[]} coordinates - An array of point coordinates
 * @property {(x:string, y:string) => void} addCoordinate - Updates the point
 * @property {addCoordinateOne} addCoordinateOne - Updates the point
 * @property {addCoordinateTwo} addCoordinateTwo - Updates the point
 */

/** @type {Shape} */
const square = {}

展示A

@property {(x:string, y:string) => void} addCoordinate - Updates the point

This作品的全部作品,但不能像我需要的那样重复使用。

展览b

/**
 * @typedef {Function} addCoordinateOne
 * @param {string} X
 * @param {string} Y
 */

这是一种的作品,因为它检测到自定义函数类型。但是它不能正确解析参数。我确保遵循@TyPedef标签的文档: https://jsdoc.app.app/tags-tags-typedef.html

/I.SSTATIC.NET/4LBHS.png“ rel =“ noreferrer”>

/**
 * @function addCoordinateTwo
 * @param {string} X
 * @param {string} Y
 */

https://i.sstatic.net/4lbhs.png 完全失败。尽管是JSDOC的@function文档推荐的方法。

https://jsdoc.app/tags-function.html

获得展览B或C像展览A一样工作?

I am trying to document custom function types as part of an object, any help would be greatly appreciated:

Context of the problem

Here is a simple object declaration with some function properties (addCoordinate, addCoordinateOne, addCoordinateTwo) which we will go over as 3 exhibits, and why none of these work.

/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */

/**
 * @typedef {Object} Shape
 * @property {Point} startCoordinate - the starting coordinate
 * @property {Point[]} coordinates - An array of point coordinates
 * @property {(x:string, y:string) => void} addCoordinate - Updates the point
 * @property {addCoordinateOne} addCoordinateOne - Updates the point
 * @property {addCoordinateTwo} addCoordinateTwo - Updates the point
 */

/** @type {Shape} */
const square = {}

Exhibit A

@property {(x:string, y:string) => void} addCoordinate - Updates the point

This works fully, but isn't reusable like I need.

Exhibit A

Exhibit B

/**
 * @typedef {Function} addCoordinateOne
 * @param {string} X
 * @param {string} Y
 */

This sort of works, as it detects the custom function type. But it doesn't parse parameters properly. I made sure to follow the documentation for the @typedef tag:
https://jsdoc.app/tags-typedef.html

Exhibit B

Exhibit C

/**
 * @function addCoordinateTwo
 * @param {string} X
 * @param {string} Y
 */

This fails completely. Despite being the recommended approach by JSDoc's @function documentation.
https://jsdoc.app/tags-function.html

The Question

Is there any way I could get Exhibit B or C working like Exhibit A?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2025-01-29 09:10:25

您可以将展览a更改为typedef:

/** @typedef {(x:string, y:string) => void} addCoordinate */

编辑:或逐字化,可以使用 @callback标签:

/** 
 * @callback addCoordinate
 * @param {number} x - The x coordinate
 * @param {number} y - The y coordinate 
 * @returns {void}
 */

然后根据需要重复使用:


/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */

/**
 * @typedef {Object} Shape
 * @property {Point} startCoordinate - the starting coordinate
 * @property {Point[]} coordinates - An array of point coordinates
 * @property {addCoordinate} addCoordinate - Updates the point
 * @property {addCoordinate} addCoordinateOne - Updates the point
 * @property {addCoordinate} addCoordinateTwo - Updates the point
 */

结果:

如果使用速记语法,则没有paramater说明可用。

如果使用@Callback语法,则为每个Paramater提供了描述:

You can change Exhibit A to a typedef:

/** @typedef {(x:string, y:string) => void} addCoordinate */

Edit: Or more verbosely, you can use the @callback tag:

/** 
 * @callback addCoordinate
 * @param {number} x - The x coordinate
 * @param {number} y - The y coordinate 
 * @returns {void}
 */

Then reuse as needed:


/**
 * @typedef {Object} Point
 * @property {number} x - The X Coordinate
 * @property {number} y - The Y Coordinate
 */

/**
 * @typedef {Object} Shape
 * @property {Point} startCoordinate - the starting coordinate
 * @property {Point[]} coordinates - An array of point coordinates
 * @property {addCoordinate} addCoordinate - Updates the point
 * @property {addCoordinate} addCoordinateOne - Updates the point
 * @property {addCoordinate} addCoordinateTwo - Updates the point
 */

Result:

If using the shorthand syntax, no paramater descriptions are available.
vs code hover showing expected Intellisense hint

If using the @callback syntax, descriptions are provided for each paramater:
vs code hover showing Intellisense hint with param description

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文