Skip to content

Instantly share code, notes, and snippets.

@MeyCry
Last active March 31, 2020 14:11
Show Gist options
  • Save MeyCry/ac762fd128b8c090cc7d9acdb23f47e0 to your computer and use it in GitHub Desktop.
Save MeyCry/ac762fd128b8c090cc7d9acdb23f47e0 to your computer and use it in GitHub Desktop.
TypeScript Returned Value From Object Method By Key
/**
* example:
* const aObj = {
* b: () => 42,
* c: () => "hello"
* }
* type ObjExample = ReturnedValueFromObjectMethodByKey<typeof aObj>;
* ObjExample type will be: {b: number; c: string}
*/
export type ReturnedValueFromObjectMethodByKey<T> = {
[K in keyof T]: T[K] extends (...args: any) => any ? ReturnType<T[K]> : T[K];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment