Skip to content

Instantly share code, notes, and snippets.

@RobinRadic
Last active December 28, 2018 12:10
Show Gist options
  • Save RobinRadic/085109ca6eee9a471d8b1204bdfec0cd to your computer and use it in GitHub Desktop.
Save RobinRadic/085109ca6eee9a471d8b1204bdfec0cd to your computer and use it in GitHub Desktop.
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