nshanmugamram
8/15/2017 - 1:08 PM

Replace interfaces with mixin


class User implements NamingObject {
    name: string;
    public setName : (name: string) => void;
    public getName : ()  => string;
}

class NamingObject {
    name: string;
    public setName(name:string) {
        this.name =  name;
    }
    public getName() {
        return this.name;
    }
}

class NamingObject2 {
    name: string;
    public setName(name:string) {
        this.name =  name+"@"+name;
    }
    public getName() {
        return this.name;
    }
}
function applyMixins(derivedCtor: any, baseCtors: any[]) { 
    baseCtors.forEach(baseCtor => {
        Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
            console.log(name);
            derivedCtor.prototype[name] = baseCtor.prototype[name];
        })
    });
}
 
applyMixins(User,[NamingObject2]);

var user = new User();
user.setName("Nithyeswari");