Typescript: Generic Promise type + async function fails with is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value

Created on 8 Mar 2019  路  1Comment  路  Source: microsoft/TypeScript

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

  • Error: TS1055: Type 'Promise' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. #26781
  • generic promise type
  • generic promise interface
  • generic promise async

Code

// This works:
const getUserId = async (): Promise<{ error: null, data: number }> => {
  return {
    error: null,
    data: 1
  }
}

// Using generics doesn't work: 

type ApiError = {
  message: string
}

type ApiResponse<T> = Promise<{
  error: null | ApiError;
  data: T
}>

// Fails with: Type 'ApiResponse' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
const getUserIdGeneric = async (): ApiResponse<string> => {
  // some async stuff
  return {
    error: null,
    data: '1'
  }
}


Expected behavior:
The compiler should understand generic Promise types.

Actual behavior:
The compiler doesn't understand generic Promise types used together with async function.

Playground Link:
Playground link here.

Related Issues:
https://github.com/Microsoft/TypeScript/issues/26781

Most helpful comment

My bad. I forgot to configure "target": "es2015" in my tsconfig.json. :)

>All comments

My bad. I forgot to configure "target": "es2015" in my tsconfig.json. :)

Was this page helpful?
0 / 5 - 0 ratings