Created
February 7, 2020 15:45
-
-
Save neolitec/2ecd95c9a1424d2425cdb4cc35cd6e2a to your computer and use it in GitHub Desktop.
ReturnTypes.ts
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
class Foo { | |
name = 'bar'; | |
fun1(): number { | |
return 12; | |
} | |
async func2(): Promise<{foo: string}> { | |
return {foo: 'foo'}; | |
} | |
} | |
type FooTypes = ReturnTypes<Foo>; | |
// => | |
// type FooTypes = { | |
// name: string; | |
// fun1: number; | |
// func2: { | |
// foo: string; | |
// }; | |
// } |
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
export type ReturnTypes<T> = { | |
[P in keyof T]: T[P] extends (...args: any[]) => any | |
? ReturnType<T[P]> extends PromiseLike<infer U> | |
? U | |
: ReturnType<T[P]> | |
: T[P]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment