从函数返回类型分配类型的正确方法是什么?
async function getFood(){
const food = await {fruit: 'banana', qty: 3}
return food
}
/**
* made up syntax
* @type {returnOf getFood}
* */
let doesntWork;
/** @type {{fruit: string, qty: number}} */
let worksButNoInferring
let worksButRequiresCallingFunction = getFood()
Run Code Online (Sandbox Code Playgroud)
VSCode 证明,返回类型是可用的。我只是不知道如何在不调用该函数的情况下获取它。
小智 9
它可能不符合 JSDoc 规范,但 VS Code 支持 JSDoc 中的 Typescript 语法。
async function getFood(){
const food = await {fruit: 'banana', qty: 3}
return food
}
/** @type { typeof getFood extends (...args: any[]) => infer U ? U : any } */
let test;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1488 次 |
| 最近记录: |