Created
March 12, 2019 11:59
-
-
Save ackvf/45bd01de261438597012abe7a7a55cde to your computer and use it in GitHub Desktop.
Extracting Arguments type of an abstract action executor method
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
// 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