Skip to content

Instantly share code, notes, and snippets.

@ackvf
Created March 12, 2019 11:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ackvf/45bd01de261438597012abe7a7a55cde to your computer and use it in GitHub Desktop.
Save ackvf/45bd01de261438597012abe7a7a55cde to your computer and use it in GitHub Desktop.
Extracting Arguments type of an abstract action executor method
// Class-based Tree Shaking - HTTP203 @ Google Chrome Developers
// https://www.youtube.com/watch?v=lsd2-TCgHEs&t=599
class Store {
/**
* Do the given action with the given parameters.
*
* @param {A => R} action Action to be taken.
* @param {A} args Arguments to be passed to the action.
* @returns {R} Returns the result of original action call.
*/
public do<A extends any[], R>(
action: (...args: A) => R,
...args: A
): R {
return action.apply(this, args);
}
}
const set = (prop: string, value: number) => `Set ${prop} = ${value}`;
const get = (prop: string) => 42;
/* ------- */
const store = new Store();
let v = store.do(set); // ...args: [string, number] => string
let g = store.do(get); // ...args: [string] => number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment