Last active
December 26, 2021 19:07
-
-
Save debkanchan/3456d76feee19311acb1446b79a9faac to your computer and use it in GitHub Desktop.
Get Promised return type of an async function in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TS 4.5 announces
Awaitedkeyword which does the same thing but better please use that now