typescript - 为什么不能使用“type A = Promise" 作为异步函数的返回类型?
问题描述
async function AsyncFunction(): Promise<number> {
return 0;
}
按预期工作没有问题;
async function AsyncFunction(): AsyncFunctionReturnType {
return 0;
}
type AsyncFunctionReturnType = Promise<number>
投掷"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 vs ES2015 target
推荐阅读
- javascript - 使用JS函数代替重复代码
- html - 具有四个 div 的 Flex 容器,需要三列,第二列有两行
- css - 内联块和文本对齐不在同一个 div 中工作
- laravel - Laravel - 对都属于某物的用户和对象的策略授权
- node.js - 节点 - 如何在 --watch 期间运行并行命令?
- php - 根据对象内的项目从对象数组中删除项目
- azure - ARM 模板应用服务配置 - 竞争条件/不一致的行为
- django - 如何解决具有多对多关系的石墨烯 django 节点字段
- java - 从 recyclerview 项目中将选定的 RadioButton 的文本保存在模型类中
- c# - 具有两个独立表和一个依赖表的多个一对一关系