Skip to content

Instantly share code, notes, and snippets.

@Soul-Master
Created March 10, 2018 17:13
Show Gist options
  • Save Soul-Master/d3076faaa6dc292ab44b97549de77ea8 to your computer and use it in GitHub Desktop.
Save Soul-Master/d3076faaa6dc292ab44b97549de77ea8 to your computer and use it in GitHub Desktop.
/*
It's impossible to call constructor of mixin class. All default properties in TypeScript don't work.
*/
function applyMixins<T>(derivedCtor: any, ...baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
return <Constructor<T>>derivedCtor;
}
export class BaseFeature {
width = 100;
height = 100;
resize(percent: number) {
console.log('resize to: ' + percent);
return true;
}
}
export abstract class ClosableObject {
isClosed = false;
close() {
console.log('close object');
}
}
export abstract class MovableObject {
isMovable = true;
move(top: number, left: number) {
console.log('move to: ' + top + ', ' + left);
}
}
export interface FullFeature extends ClosableObject, MovableObject { }
export class FullFeatureObject extends applyMixins<FullFeature>(ClosableObject, MovableObject) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment