Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Last active January 10, 2019 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oieduardorabelo/6fc4f0fb50df1a51d6b05e104f303be4 to your computer and use it in GitHub Desktop.
Save oieduardorabelo/6fc4f0fb50df1a51d6b05e104f303be4 to your computer and use it in GitHub Desktop.
// 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