Last active
January 10, 2019 13:59
-
-
Save oieduardorabelo/6fc4f0fb50df1a51d6b05e104f303be4 to your computer and use it in GitHub Desktop.
This file contains 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
// ts 3.x | |
// https://stackoverflow.com/questions/54129289/typescript-call-a-member-of-an-object-with-arguments | |
let fns = { | |
set: (foo: string, bar: string) => { | |
return { foo, bar }; | |
} | |
}; | |
function callMethodWithArgs< | |
FnsObj extends { [key in MethodKey]: (...args: any[]) => any }, | |
MethodKey extends string, | |
FnInMethodKey extends FnsObj[MethodKey], | |
>(obj: FnsObj, methodName: MethodKey, args: Parameters<FnInMethodKey>) { | |
return obj[methodName](...args) as ReturnType<FnInMethodKey>; | |
} | |
const foobar = callMethodWithArgs(fns, "set", ["foo", "bar"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment