vindatu
vindatu

Reputation: 23

Why it is not possible to use "type A = Promise<any>" as a return type for an async function?

async function AsyncFunction(): Promise<number> {
  return 0;
}

Works without without problem as expected;


async function AsyncFunction(): AsyncFunctionReturnType {
  return 0;
}

type AsyncFunctionReturnType = Promise<number>

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


They look identical to me, why syntax matters here?

Upvotes: 2

Views: 56

Answers (1)

Elias Schablowski
Elias Schablowski

Reputation: 2812

You need to include 'es2015' in your lib for 'ES5' or 'ES3' targets. There is no difference in the syntax - but rather the target that you compiled to: Playground with error vs ES2015 target

Upvotes: 2

Related Questions