Mixins
Mixins are a faux-multiple inheritance pattern for classes
in JavaScript which TypeScript has support for. The pattern
allows you to create a class which is a merge of many
classes.
To get started, we need a type which we'll use to extend
other classes from. The main responsibility is to declare
that the type being passed in is a class.
// Then we can create a series of classes which extend
the final class by wrapping it. This pattern works well
when similar objects have different capabilities.
This mixin adds a scale property, with getters and setters
for changing it with an encapsulated private property:
type Constructor = new (...args: any[]) => {};
// This mixin adds extra methods around alpha composition
something which modern computers use to create depth:
function Scale
// Here we create two different types of sprites
which have different capabilities:
function Alpha
// Creating instances of these classes shows that
the objects have different sets of properties
and methods due to their mixins:
const ModernDisplaySprite = Alpha(Scale(Sprite));
const EightBitSprite = Scale(Sprite);
// Fails because an EightBitSprite does not have
the mixin for changing alphas:
const flappySprite = new ModernDisplaySprite("Bird");
flappySprite.x = 10;
flappySprite.y = 20;
flappySprite.setVisible();
flappySprite.setScale(0.8);
console.log(flappySprite.scale);
const gameBoySprite = new EightBitSprite("L block");
gameBoySprite.setScale(0.3);
// If you want to make more guarantees over the classes
which you wrap, you can use a constructor with generics.
gameBoySprite.setAlpha(0.5);
// Now you can declare that this mixin can only be
applied when the base class is a certain shape.
type GConstructor
// We can then create a mixin which relies on the function
present in the parameter to the GConstructor above.
type Moveable = GConstructor<{ setXYAcceleration: (x: number, y: number) => void }>;
// We cannot create this sprite until there is a class
in the mixin hierarchy which adds setXYAcceleration:
function Jumpable
const UserSprite = new Jumpable(ModernDisplaySprite);