自定义“@typedef”对象或“未定义”

发布于 2025-01-13 19:04:15 字数 2089 浏览 2 评论 0原文

我试图正确记录我正在参与的程序中发生的场景。我不确定如何正确表达某些承诺的预期解决方案。下面是我尝试嘲笑正在发生的事情。

有一些 api 承诺超出了我试图记录的程序范围。我提到这一点是因为记录 api 方法不是一个选项,而是包含这些承诺的解析值的变量才是我想要关注的地方。

// simplified example
function apiMethod() {
    const fail = 0;
    const response = {
        error: fail ? "error message" : undefined, // undef if no errors
        success: fail ? undefined : [ // undef if errors present
            {key01: "val01", key02: 1, key03: {"subKey01": [1, 2, 3]}},
            {key01: "val03", key02: 2, key03: {"subKey01": [4, 5, 6]}}
        ]
    };
    return new Promise(resolve => setTimeout(() => resolve(response), 250));
}
(async () => {
    /**
     * @typedef {object} thing
     * @property {string} key01
     * @property {number} key02
     * @property {{subKey02: string[]}} key03
     */
    /**
     * @type {Promise<{success: thing[]|undefined, error: string|undefined}>}
     */
    const myVar = await apiMethod();
    console.log(myVar.key01);
})();

这似乎工作得很好,但我遇到的问题是在某些情况下 success 属性可能是未定义的。现在,这就是将鼠标悬停在 myVar 上时 vscode 工具提示的外观;

const myVar: Promise<{
    success: thing[];
    error: string | undefined;
}>

注意缺少 undefined 表示成功

这是将 thing 悬停在 @type 表达式中时 vscode 工具提示的外观:

type thing = {
    key01: string;
    key02: number;
    key03: {
        subKey02: string[];
    };
}

问题:

  1. 是否可以表示success将是thing对象的数组或未定义?当使用 typedef 时,我似乎无法做到这一点。

  2. 目前,似乎只有将 thing 悬停在 @type 表达式中时才能看到 thing 对象的分解视图。当我将鼠标悬停在 myVar 上时,它只是显示 success 将是一个 thing 数组。悬停在 myVar 上时是否可以显示 thing 的“分解”视图,同时仍然使用 @typedef

I am trying to properly document a scenario that happens throughout out a program I am working in. I am unsure how to properly express the expected resolution of certain promises. Below is my attempt to mock what is happening.

There are certain api promises, outside the scope of the program that I am trying to document. I mentioned this because documenting the api methods is not an option, but rather the variable that contain the resolved values of those promises is where I'd like focus.

// simplified example
function apiMethod() {
    const fail = 0;
    const response = {
        error: fail ? "error message" : undefined, // undef if no errors
        success: fail ? undefined : [ // undef if errors present
            {key01: "val01", key02: 1, key03: {"subKey01": [1, 2, 3]}},
            {key01: "val03", key02: 2, key03: {"subKey01": [4, 5, 6]}}
        ]
    };
    return new Promise(resolve => setTimeout(() => resolve(response), 250));
}
(async () => {
    /**
     * @typedef {object} thing
     * @property {string} key01
     * @property {number} key02
     * @property {{subKey02: string[]}} key03
     */
    /**
     * @type {Promise<{success: thing[]|undefined, error: string|undefined}>}
     */
    const myVar = await apiMethod();
    console.log(myVar.key01);
})();

This seems to work mostly well, but where I am getting stuck on is expressing that the success property could be undefined under certain circumstances. Right now this is how the vscode tooltip looks when hovering the myVar;

const myVar: Promise<{
    success: thing[];
    error: string | undefined;
}>

Note the missing undefined for success

This is how the vscode tooltip looks when hovering thing in the @type expression:

type thing = {
    key01: string;
    key02: number;
    key03: {
        subKey02: string[];
    };
}

Questions:

  1. Is is possible to express that success will be an array of thing objects or undefined? When using typedef it doesn't seem like I can do this.

  2. Right now it only seems possible to see the exploded view of the thing object when hovering thing in the @type expression. When I hover myVar it simply shows that success will be an array of things. Is it possible show this "exploded" view of thing when hovering myVar, whilst still using @typedef?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文