Skip to content

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 #30272

@skovhus

Description

@skovhus

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

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:
#26781

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions