Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Constructor<T> = new(...args: any[]) => T;
- function mixin<T>(MixIn: Constructor<T>) {
- return function decorator<U>(Base: Constructor<U>) : Constructor<T & U> {
- Object.getOwnPropertyNames(MixIn.prototype).forEach(name => {
- Base.prototype[name] = MixIn.prototype[name];
- });
- return Base as Constructor<T & U>;
- }
- }
- class MixInClass {
- mixinMethod() {console.log('mixin method is called')}
- }
- /**
- * apply mixin(MixInClass) implicitly (use decorator syntax)
- */
- @mixin(MixInClass)
- class Base1 {
- baseMethod1() { }
- }
- const m1 = new Base1();
- m1.baseMethod1();
- m1.mixinMethod(); // error TS2339: Property 'mixinMethod' does not exist on type 'Base1'.
- /**
- * apply mixin(MixInClass) explicitly
- */
- class Base2 {
- baseMethod2() { }
- }
- const MixedBase2 = mixin(MixInClass)(Base2);
- const m2 = new MixedBase2();
- m2.baseMethod2();
- m2.mixinMethod(); // OK
- //...
- var Base1 = /** @class */ (function () {
- function Base1() {
- }
- Base1.prototype.baseMethod1 = function () { };
- Base1 = __decorate([
- mixin(MixInClass)
- ], Base1);
- return Base1;
- }());
- //...
Add Comment
Please, Sign In to add comment