Skip to content

Instantly share code, notes, and snippets.

@awerlang
Created November 16, 2016 15:31
Show Gist options
  • Save awerlang/590dccc22ac8f00b00b8ca782fa1a51c to your computer and use it in GitHub Desktop.
Save awerlang/590dccc22ac8f00b00b8ca782fa1a51c to your computer and use it in GitHub Desktop.
/**
Mixes in objects in `properties` into a class.
To be used as a decorator.
@mixin({...})
class MyClass {...}
*/
function mixin(properties: {[key: string]: any}): Function {
if (properties == null) {
throw new Error(`Argument for 'properties' is required.`);
}
return function (target: any): any {
if (typeof properties === 'object') {
Object.keys(properties).forEach(name => {
target.prototype[name] = properties[name];
});
}
return target;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment