Skip to content

Instantly share code, notes, and snippets.

@debkanchan
Last active December 26, 2021 19:07
Show Gist options
  • Save debkanchan/3456d76feee19311acb1446b79a9faac to your computer and use it in GitHub Desktop.
Save debkanchan/3456d76feee19311acb1446b79a9faac to your computer and use it in GitHub Desktop.
Get Promised return type of an async function in TypeScript
type PromisedReturnType<T extends (...args: any) => any> = T extends (...args: any[]) => PromiseLike<infer R> ? R : any
//usage
async function f1() {
return "Value";
}
async function f2(): Promise<number> {
return Promise.resolve(5);
}
type returnTypeoff1 = PromisedReturnType<typeof f1>; //string
type returnTypeoff2 = PromisedReturnType<typeof f2>; //number
@debkanchan
Copy link
Author

TS 4.5 announces Awaited keyword which does the same thing but better please use that now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment