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:
https://github.com/Microsoft/TypeScript/issues/26781
My bad. I forgot to configure "target": "es2015" in my tsconfig.json. :)
Most helpful comment
My bad. I forgot to configure
"target": "es2015"in my tsconfig.json. :)