gpt4 book ai didi

jsdoc - 为多种功能重用文档?

转载 作者:行者123 更新时间:2023-12-04 15:39:18 26 4
gpt4 key购买 nike

我有一个包含大量选项的函数:

/**
* Show dialog in a blocking manner.
*
* @param {object} opts
* @param {string} opts.msg "Body" of the dialog.
* @param {number} opts.timeout Seconds - floating point values are rounded. (ActiveX imposes this)
* @param {string} opts.title Title of the dialog.
* @param {number} opts.icon Use constants for this. (See docs)
* @param {number} opts.buttons Use constants for this. (See docs)
* @param {number} opts.defaultButton Use constants for this. (See docs)
* @returns {number} Use our constants to check for what the user chose.
*/
const showSync = (opts) => {
...
}

但我也有这个函数的非阻塞版本,显然采用相同的选项并返回一个 Promise。复制/粘贴文档似乎很脏,因为这会降低可维护性和意外不一致的可能性。

因此,最好的是以下内容:
/**
* Shows dialog in a non-blocking manner.
*
* @inheritdoc showSync
* @returns {Promise<number>} Use our constants to check for what the user chose.
*/
const show = (opts) => {
...
}

这有可能吗?

[更新]

这不是 JSDoc for reused Function interface 的副本因为这个问题仅仅是关于重用相同的定义,而这个问题是关于重用但也部分地覆盖了该定义。因此,那里的答案并没有回答这里的问题。

最佳答案

我认为用 jsdoc 做到这一点的最好方法是这样的:

/**
* Options for showing a dialog.
* @typedef {Object} ShowOptions
* @property {string} msg "Body" of the dialog.
* @property {number} timeout Seconds - floating point values are rounded. (ActiveX imposes this)
* @property {string} title Title of the dialog.
* @property {number} icon Use constants for this. (See docs)
* @property {number} buttons Use constants for this. (See docs)
* @property {number} defaultButton Use constants for this. (See docs)
*/

/**
* Show dialog in a blocking manner.
*
* @param {ShowOptions} opts
* @returns {number} Use our constants to check for what the user chose.
*/
const showSync = (opts) => {...}

/**
* Shows dialog in a non-blocking manner.
*
* @param {ShowOptions} opts
* @returns {Promise<number>} Use our constants to check for what the user chose.
*/
const show = (opts) => {...}

您可以更进一步,并将类似的概念也应用于返回值:

/**
* Use our constants to check for what the user chose.
* @typedef {number} ShowResult
*/

/**
* Show dialog in a blocking manner.
*
* @param {ShowOptions} opts
* @returns {ShowResult}
*/
const showSync = (opts) => {...}

/**
* Shows dialog in a non-blocking manner.
*
* @param {ShowOptions} opts
* @returns {Promise<ShowResult>}
*/
const show = (opts) => {...}

关于jsdoc - 为多种功能重用文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51135388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com