foibles
Version:
Composition and mixins and TypeScript classes
31 lines • 767 B
JavaScript
/**
* Take the given base class and turn it into an extendable class, giving it
* a static method `with` that can be used to apply mixins.
*
* When using TypeScript this should be accompanied with a type definition
* using `Extendable`:
*
* ```typescript
* type ExampleBaseType = Extendable<typeof ExampleBaseType>;
* const ExampleBaseType = toExtendable(class ExampleBaseType {
* ...
* });
* ```
*
* @param o
*/
export function toExtendable(o) {
Object.defineProperty(o, 'with', {
enumerable: false,
value: mixer
});
return o;
}
const mixer = function (...mixins) {
let result = this;
for (const mixin of mixins) {
result = mixin(result);
}
return result;
};
//# sourceMappingURL=extendable.js.map