TypeScript: 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.

I already read this https://github.com/Microsoft/TypeScript/issues/12776#issuecomment-265885846 but is not possible to fix. Or I can’t understand how has even been solved.

Is fixed by adding const AsyncBool = Promise; but is not possible in my case scenario. Also where can I read about this strange syntax. A type and a const with the same name?! Looks strange and confusing.


async function foo(): Promise<boolean> {
    return Promise.resolve(true);
}

type AsyncBool = Promise<boolean>;
const AsyncBool = Promise;
async function bar(): AsyncBool {
    return Promise.resolve(true);
}

Code

interface O {
  o: Promise<number>,
}

const myFn = async ():O["o"] => {
  
}

Expected behavior:

No error.

Actual behavior:

Error: TS1055: Type ‘Promise<number>’ is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

Playground Link:

http://www.typescriptlang.org/play/#src=interface O { o%3A Promise<number>%2C } const myFn %3D async ()%3AO["o"] %3D> { }

(by the way don’t search “ts playground” on google. TS team needs to invest in some SEO I guess.)

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 33
  • Comments: 39 (5 by maintainers)

Most upvoted comments

This issue has been marked as ‘Question’ and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you’re still waiting on a response, questions are usually better suited to stackoverflow.

I’m having the same issue with axiosAxiosPromise typing (see: https://github.com/axios/axios/blob/master/index.d.ts#L87 )

I’m getting:

Type 'AxiosPromise' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value. ts(1055)

As you can see in the provided link, it’s just:

export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {}

Here’s the code I use:

const getAsync = async (): AxiosPromise<any> => {
  ...
}

If I copy the type, it works as expected:

const getAsync = async (): Promise<AxiosResponse<any>> => {
  ...
}

Who should we tag to reopen this, I found only one name?

Hello, @RyanCavanaugh, this is not a question, but clearly a bug that needs fixing. If it’s not a duplicate, please change the issue type and re-open.

Regards

It seems to me that if this expression is valid typescript:

const f = <T> async ():Promise<T> => {...}

(and it is)… then the following expression should also be valid typescript:

type P<T> = Promise<T>
const f = <T> async ():P<T> => {...}

the nature of the implementation of choosing a promise-compatible constructor to use during desugaring seems to be, or at least seems like it should be, an independent concern that doesn’t bear on the case where the return type aliases directly and inarguably to the already acceptable Promise type.

Said differently, In cases where the literal word Promise is acceptable, why shouldn’t any other type alias which resolves deterministically to it and only it be also acceptable?

Forgive me if I am overlooking something. 😃

With arrow functions it might work by moving the type declaration to the const object.

//doesn't work on target: ES5
const a = async () : Promise<any> => 3

//works for any target
const b : () => Promise<any> = async () => 3```

Don’t use "target": "es5" in tsconfig.json, use something like esnext should work!

“target”: “esnext”,

For me, this issue arose with a conditional type. I changed

const foo = <T extends boolean>(): T extends true ? Promise<string[]> : Promise<string[][]> => {
    //...
}

to

const foo = <T extends boolean>(): Promise<T extends true ? string[] : string[][]> =>{
    // ...
}

In other words, wrapping the whole conditional in a promise worked when splitting the promises inside the conditional didn’t.

The same with built-in ReturnType helper:

async function fooBar(): Promise<void> {

}

async function barBaz(): ReturnType<typeof fooBar> { // Type 'ReturnType' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

}

https://www.typescriptlang.org/play/index.html#src=async function fooBar()%3A Promise<void> { } async function barBaz()%3A ReturnType<typeof fooBar> { }