,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)"


它们看起来和我一模一样,为什么语法在这里很重要?

标签: typescript

解决方案


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


推荐阅读