"作为异步函数的返回类型?-6ren"> "作为异步函数的返回类型?-async function AsyncFunction(): Promise { return 0; } 按预期正常工作; async function AsyncFunction(): Asy-6ren">
gpt4 book ai didi

typescript - 为什么不能使用 "type A = Promise"作为异步函数的返回类型?

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

async function AsyncFunction(): Promise<number> {
return 0;
}
按预期正常工作;
async function AsyncFunction(): AsyncFunctionReturnType {
return 0;
}

type AsyncFunctionReturnType = Promise<number>
throw "Type 'AsyncFunctionReturnType' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. ts(1055)"
它们对我来说看起来相同,为什么语法在这里很重要?

最佳答案

您需要在“ES5”或“ES3”目标的库中包含“es2015”。语法没有区别 - 而是您编译到的目标:Playground with error对比 ES2015 target

关于typescript - 为什么不能使用 "type A = Promise<any>"作为异步函数的返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63145088/

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