Skip to content

Instantly share code, notes, and snippets.

@neolitec
Created February 7, 2020 15:45
Show Gist options
  • Save neolitec/2ecd95c9a1424d2425cdb4cc35cd6e2a to your computer and use it in GitHub Desktop.
Save neolitec/2ecd95c9a1424d2425cdb4cc35cd6e2a to your computer and use it in GitHub Desktop.
ReturnTypes.ts
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;
// };
// }
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