Created
May 3, 2020 01:23
-
-
Save fredyang/fb1434e62a036982033b647d714b4482 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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