1.0.0 • Published 7 years ago

ts-mixins v1.0.0

Weekly downloads
17
License
MIT
Repository
github
Last release
7 years ago

ts-mixins

Implementation of http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/ also inspired by https://github.com/likerRr/ts-mixin and https://github.com/Microsoft/TypeScript/pull/13743#issuecomment-299540915

Instead of using Object.assign or native typescript mixins, its using a custom mix function that does not call PropertyDescriptors(get/set) and applies the whole prototype chain to the mixedclass allowing for mixin classes to extend other classes

import {Mixin} from "@nan0c@ts-mixins";

export class One {

    constructor(){}

    public logO():void{
        console.log('ONE');
    }
}

export class BaseTwo {
    public logO():void{
        console.log('BASE_TWO');
    }
}

export class Two extends BaseTwo{

    constructor(){
        super();
    }

    public logO():void{
        super.logO();
        console.log('TWO');
    }
}

export class Three extends Two {
    
    constructor(){
        super();
    }

    public logO():void{
        super.logO();
        console.log('THREE');
    }
}

export interface IThreeOne extends Three,One{}

export class ThreeOne extends Mixin<IThreeOne>(Three,One) {
    constructor(){
        super();
    }

    public logO():void{
        super.logO();
        console.log('THREE_ONE');
    }
}

export class OneThree extends Mixin<IThreeOne>(One,Three) {
    
    constructor(){
        super();
    }

    public logO():void{
        super.logO();
        console.log('ONE_THREE');
    }
}

let threeOne = new ThreeOne();
threeOne.logO();
/* Will print 
   THREE_ONE 
   BASE_TWO 
   TWO
   THREE
 */
let oneThree = new OneThree();
oneThree.logO();
/* Will print 
   ONE
   ONE_THREE
 */