Introduction

Along with traditional OO hierarchies, another popular way of building up classes from reusable components is to build them by combining simpler partial classes. You may be familiar with the idea of mixins or traits for languages like Scala, and the pattern has also reached some popularity in the JavaScript community.

Mixin sample

In the code below, we show how you can model mixins in TypeScript. After the code, we’ll break down how it works.

  1. ts
    // Disposable Mixin
  2. class Disposable {
  3. isDisposed: boolean;
  4. dispose() {
  5. this.isDisposed = true;
  6. }
  7. }
  8. // Activatable Mixin
  9. class Activatable {
  10. isActive: boolean;
  11. activate() {
  12. this.isActive = true;
  13. }
  14. deactivate() {
  15. this.isActive = false;
  16. }
  17. }
  18. class SmartObject {
  19. constructor() {
  20. setInterval(
  21. () => console.log(this.isActive + " : " + this.isDisposed),
  22. 500
  23. );
  24. }
  25. interact() {
  26. this.activate();
  27. }
  28. }
  29. interface SmartObject extends Disposable, Activatable {}
  30. applyMixins(SmartObject, [Disposable, Activatable]);
  31. let smartObj = new SmartObject();
  32. setTimeout(() => smartObj.interact(), 1000);
  33. ////////////////////////////////////////
  34. // In your runtime library somewhere
  35. ////////////////////////////////////////
  36. function applyMixins(derivedCtor: any, baseCtors: any[]) {
  37. baseCtors.forEach(baseCtor => {
  38. Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
  39. Object.defineProperty(
  40. derivedCtor.prototype,
  41. name,
  42. Object.getOwnPropertyDescriptor(baseCtor.prototype, name)
  43. );
  44. });
  45. });
  46. }

Understanding the sample

The code sample starts with the two classes that will act as our mixins. You can see each one is focused on a particular activity or capability. We’ll later mix these together to form a new class from both capabilities.

  1. ts
    // Disposable Mixin
  2. class Disposable {
  3. isDisposed: boolean;
  4. dispose() {
  5. this.isDisposed = true;
  6. }
  7. }
  8. // Activatable Mixin
  9. class Activatable {
  10. isActive: boolean;
  11. activate() {
  12. this.isActive = true;
  13. }
  14. deactivate() {
  15. this.isActive = false;
  16. }
  17. }

Next, we’ll create the class that will handle the combination of the two mixins. Let’s look at this in more detail to see how it does this:

  1. ts
    class SmartObject {
  2. ...
  3. }
  4. interface SmartObject extends Disposable, Activatable {}

The first thing you may notice in the above is that instead of trying to extend Disposable and Activatable in SmartObject class, we extend them in SmartObject interface. SmartObject interface will be mixed into the SmartObject class due to the declaration merging.

This treats the classes as interfaces, and only mixes the types behind Disposable and Activatable into the SmartObject type rather than the implementation. This means that we’ll have to provide the implementation in class. Except, that’s exactly what we want to avoid by using mixins.

Finally, we mix our mixins into the class implementation.

  1. ts
    applyMixins(SmartObject, [Disposable, Activatable]);

Lastly, we create a helper function that will do the mixing for us. This will run through the properties of each of the mixins and copy them over to the target of the mixins, filling out the stand-in properties with their implementations.

  1. ts
    function applyMixins(derivedCtor: any, baseCtors: any[]) {
  2. baseCtors.forEach(baseCtor => {
  3. Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
  4. Object.defineProperty(
  5. derivedCtor.prototype,
  6. name,
  7. Object.getOwnPropertyDescriptor(baseCtor.prototype, name)
  8. );
  9. });
  10. });
  11. }