Skip to content

Instantly share code, notes, and snippets.

@fredyang
Created May 3, 2020 01:23
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 fredyang/fb1434e62a036982033b647d714b4482 to your computer and use it in GitHub Desktop.
Save fredyang/fb1434e62a036982033b647d714b4482 to your computer and use it in GitHub Desktop.
interface Constructor<T> {
new(...args: []): T;
}
interface Base {
}
interface Items {
items: [];
}
function SubClass<T extends Base>(SuperClass: Constructor<T>) {
let temp = SuperClass.prototype.log;
if (temp) {
SuperClass.prototype.log = function (...args: any) {
this.items.push(args[0]);
temp.apply(this, args);
}
}
class C extends (<Constructor<Base>>SuperClass) {
items = [];
}
return <Constructor<Items & T>>C;
}
@SubClass
class Person {
log(msg: string) {
console.log(msg);
}
}
// let P = SubClass(Person);
// let p = new P();
// p.log('hello');
// p.log('bye');
// console.log(p.items);
let p = new Person();
p.log('hello');
p.log('bye');
console.log(p['items']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment