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
Playground Link:
http://www.typescriptlang.org/play/#src=interface%20O%20%7B%0D%0A%20%20o%3A%20Promise%3Cnumber%3E%2C%0D%0A%7D%0D%0A%0D%0Aconst%20myFn%20%3D%20async%20()%3AO%5B%22o%22%5D%20%3D%3E%20%7B%0D%0A%20%20%0D%0A%7D
(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
I'm having the same issue with axios
' AxiosPromise
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>> => {
...
}
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%20function%20fooBar()%3A%20Promise%3Cvoid%3E%20%7B%0D%0A%0D%0A%7D%0D%0A%0D%0Aasync%20function%20barBaz()%3A%20ReturnType%3Ctypeof%20fooBar%3E%20%7B%0D%0A%0D%0A%7D
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.
@RyanCavanaugh why the compiler can't looking deep at the return type to understand that it's an "alias" to Promise-like type? Or this isn't a big deal and users can just change the return type to explicit Promise-like type? For me (in case of using ReturnType
or any other type alias/mapped type/etc) it looks like a bug.
@typescript-bot @timocov @hackhat
Indeed I am hitting the same issue and it's really annoying.
How do we go about reopening this?
Also are there any workarounds?
In my opinion this is definitely a bug. Also this is an even more concise test case:
async function f(): ReturnType<(...args: any) => Promise<void>> {
...
}
A rather verbose and crappy workaround I've thought(I need the self=this to use this, trying to use this with bind(this) yields another bug where this's type is inferred as any):
function f(): ReturnType<(...args: any) => Promise<void>> {
const self=this;
return (async () => {
....
})();
}
@RyanCavanaugh ping
Same issue:
export async function groupCreate(context: Context): Promise<Output> {
async (ctx): ReturnType<typeof groupCreate> => {
Same issue.
Same issue
Yep same issue!
@RyanCavanaugh can you please affirm that you will not fix it and will not consider to change that behavior (at least at the moment)?
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. :)
Same issue with GaxiosPromise
:
export declare type GaxiosPromise<T = any> = Promise<GaxiosResponse<T>>;
submitAction = async (action: SubmitAction): GaxiosPromise<sheets_v4.Schema$BatchUpdateSpreadsheetResponse> => { ... }
Error: TS1055: Type 'GaxiosPromise' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Same issue:
/* types */
export interface ModalResultPayload {
action: ModalUserAction
params: ModalUserParams
}
export type ModalResultPromise = Promise<ModalResultPayload>
/* error location */
export async function callModal(renderFunction: ModalFC): ModalResultPromise { ... }
And this works:
export async function callModal(renderFunction: ModalFC): Promise<ModalResultPayload> { ... }
Same here
const AsyncFunc = (): Promise<void> => Promise.resolve()
const ReturnAsyncFunc = (): ReturnType<typeof AsyncFunc> => AsyncFunc()
reports error 1055
help!
halp!
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
Most helpful comment
I'm having the same issue with
axios
'AxiosPromise
typing (see: https://github.com/axios/axios/blob/master/index.d.ts#L87 )I'm getting:
As you can see in the provided link, it's just:
Here's the code I use:
If I copy the type, it works as expected: