Last active
December 28, 2018 12:10
-
-
Save RobinRadic/085109ca6eee9a471d8b1204bdfec0cd to your computer and use it in GitHub Desktop.
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
export function Mixin<T>(...mixins: Array<new (...args: any[]) => any>): new (...args: any[]) => T { | |
return mixins.reduceRight((prev, cur) => __extends(cur, prev), class {}); | |
} | |
function __extends(f: MixinFunction, s: MixinFunction): MixinFunction { | |
const mixedClass = class { | |
constructor() { | |
return f.apply(s.apply(this, arguments) || this, arguments); | |
} | |
}; | |
void Object.assign(mixedClass.prototype, f.prototype, s.prototype); | |
for (const p in s) if (s.hasOwnProperty(p)) mixedClass[p] = s[p]; | |
for (const p in f) if (f.hasOwnProperty(p)) mixedClass[p] = f[p]; | |
return mixedClass; | |
} | |
interface MixinFunction { | |
new (...args: any[]): any; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment