Table of contents
Introduction
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.
Understanding the sample
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:
The first thing you may notice in the above is that instead of trying to extend and Activatable
in class, we extend them in SmartObject
interface. 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.
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.