文档过载功能/方法

发布于 2025-02-11 16:26:16 字数 1196 浏览 1 评论 0原文

我一直在尝试使用JSDOC在JS中记录过载功能:

有2个用例:

assignSlave(ticket, userid);
assignSlave(ticket, firstname, lastname);

我想在VSCODE中看起来像这样:

  • 案例1

    “案例1”

  • 案例2

    “

等等...

我尝试了 document in JSDOC中的载荷功能 ,但对我不起作用:

/**
 * Test
 *
 * @function assignSlave
 * @param {String} ticket
 * @param {String} userid
 *//**
 * Test2
 *
 * @function assignSlave
 * @param {String} ticket
 * @param {String} firstname
 * @param {String} lastname
 */
function assignSlave(a, b, c){}
assignSlave()

我明白了:

”

.sstatic.net /

我阅读本文,但不确定在我的情况下它是如何工作的。

I've been trying to document an overload function in JS using JSDoc:

There's 2 use cases:

assignSlave(ticket, userid);
assignSlave(ticket, firstname, lastname);

I'd like to have it look like this in VSCode:

  • Case 1

    Case 1

  • Case 2

    Case 2

And so on...

I tried the solution given in Document overloaded function in JSDoc but it didn't work for me:

/**
 * Test
 *
 * @function assignSlave
 * @param {String} ticket
 * @param {String} userid
 *//**
 * Test2
 *
 * @function assignSlave
 * @param {String} ticket
 * @param {String} firstname
 * @param {String} lastname
 */
function assignSlave(a, b, c){}
assignSlave()

I get this:

this

Is there a way to achieve what I'm trying to do?

I read this article but am not sure how it works in my case.

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

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

发布评论

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

评论(2

总攻大人 2025-02-18 16:26:16

在Typescript 5中,您可以使用新的@overload标签:

/**
 * @overload
 * @param {string} ticket
 * @param {string} userId
 *//**
 * @overload
 * @param {string} ticket
 * @param {string} firstname
 * @param {string} lastname
 *//**
 * @param {string} a
 * @param {string} b
 * @param {string} c
 */
function assignSlave(a, b, c) {}

用于参考: https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-0/#overload-support-support-ipport-in-jsdoc

In TypeScript 5, you can use the new @overload tag:

/**
 * @overload
 * @param {string} ticket
 * @param {string} userId
 *//**
 * @overload
 * @param {string} ticket
 * @param {string} firstname
 * @param {string} lastname
 *//**
 * @param {string} a
 * @param {string} b
 * @param {string} c
 */
function assignSlave(a, b, c) {}

For reference: https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#overload-support-in-jsdoc

情话墙 2025-02-18 16:26:16

像以下片段一样做。从函数更改为箭头函数并相应地设置其类型。最后一件事是为尾随参数分配一个默认值,以便彼此兼容过载类型。

/**
 * @type  {{
 *   (ticket: string, userid: string): void;
 *   (ticket: string, firstname: string, lastname: string): void;
 * }}
 */
 const assignSlave = (a, b, c = '') => {}
 assignSlave('ticket', 'userid')
 assignSlave('ticket', 'firstname', 'lastname')

这样可以用2个重载来识别功能:

Overload 1

​“ https://i.sstatic.net/yn2mql.png” alt =“ Overload 2”>

Do it like the following snippet. Change from function to an arrow function and set its type accordingly. The last thing is to assign a default value to trailing arguments so the overload types are compatible with each other.

/**
 * @type  {{
 *   (ticket: string, userid: string): void;
 *   (ticket: string, firstname: string, lastname: string): void;
 * }}
 */
 const assignSlave = (a, b, c = '') => {}
 assignSlave('ticket', 'userid')
 assignSlave('ticket', 'firstname', 'lastname')

This way the function is recognized with 2 overloads:

Overload 1

Overload 1

Overload 2

Overload 2

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