自定义“@typedef”对象或“未定义”
我试图正确记录我正在参与的程序中发生的场景。我不确定如何正确表达某些承诺的预期解决方案。下面是我尝试嘲笑正在发生的事情。
有一些 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[];
};
}
问题:
是否可以表示
success
将是thing
对象的数组或未定义?当使用typedef
时,我似乎无法做到这一点。目前,似乎只有将
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:
Is is possible to express that
success
will be an array ofthing
objects or undefined? When usingtypedef
it doesn't seem like I can do this.Right now it only seems possible to see the exploded view of the
thing
object when hoveringthing
in the@type
expression. When I hovermyVar
it simply shows that success will be an array ofthing
s. Is it possible show this "exploded" view ofthing
when hoveringmyVar
, whilst still using@typedef
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论