Mixins
Compose with functions
A mixin is a function that takes a base class and returns an extended class. Each mixin adds one capability, such as logging or timestamps, without editing the original.
Because mixins are plain functions, you combine them by nesting calls. The result reads as a recipe: start from a base, then layer each behavior in order. The program applies one mixin to an empty base class and prints the field it added.
type Ctor = new (...args: any[]) => {}; function Tagged<T extends Ctor>(Base: T) { return class extends Base { tag = "card"; }; } class Base {} const TaggedBase = Tagged(Base); console.log(new TaggedBase().tag);
card
Apply to classes
Applying means passing your class through the mixin call. The returned class has all old members plus the new field or method, fully typed for later use.
Keep the base tiny and let mixins supply cross cutting extras. Tests can then cover the base alone and each mixin alone with no tangled setup. The program builds a card through the mixin and prints both the old title and the new tag.
type Ctor = new (...args: any[]) => {}; function Tagged<T extends Ctor>(Base: T) { return class extends Base { tag = "card"; }; } class Card { title = "Draft"; } const TaggedCard = Tagged(Card); const one = new TaggedCard(); console.log(one.tag, one.title);
card Draft
Name conflicts
Two mixins can add the same member name with different meaning. The last one applied wins silently, which is a classic source of confusing bugs.
Prevent this with distinct names, such as createdLabel versus updatedLabel. Explicit names cost a few letters and save long debugging sessions. The program prints the inherited label next to the name the child class chose.
class Base { label = "base"; } class Child extends Base { createdLabel = "made"; } const child = new Child(); console.log(child.label, child.createdLabel);
base made
When mixins win
Mixins win when several unrelated classes need the same small behavior. One shared function beats copy pasted methods scattered across the codebase.
Prefer plain composition or a helper function when only one class needs the logic. Reach for a mixin when the second and third consumers appear. The program layers two mixins over one base and prints both of their fields.
type Ctor = new (...args: any[]) => {}; function Tagged<T extends Ctor>(Base: T) { return class extends Base { tag = "card"; }; } class Card { title = "Draft"; } function withStamp<T extends Ctor>(Base: T) { return class extends Base { stamped = true; }; } const StampedCard = withStamp(Tagged(Card)); const one = new StampedCard(); console.log(one.tag, one.stamped);
card true
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a mixin function return?
Answer
A class that extends the base it was given
The returned class carries every member of the base plus the one the mixin adds.
-
How do you apply two mixins to one class?
Answer
Nest the calls,
second(first(Base))Each call takes the class the previous call returned, so the order of the calls is the order of the layers.
-
What happens when two mixins add the same member name?
Answer
The last one applied wins, silently
The later class overwrites the member, which is why the lesson asks for distinct names.
-
When is a helper function better than a mixin?
Answer
When only one class needs the behavior
A mixin pays off with several unrelated classes; for one consumer a plain function is simpler.
-
What does this program print?
type Ctor = new (...args: any[]) => {}; function Tagged<T extends Ctor>(Base: T) { return class extends Base { tag = "card"; }; } class Base {} const TaggedBase = Tagged(Base); console.log(new TaggedBase().tag);Answer
card
The returned class carries the tag field, and the instance prints it.
-
What does this program print?
class Base { label = "base"; } class Child extends Base { createdLabel = "made"; } const child = new Child(); console.log(child.label, child.createdLabel);Answer
base made
The child inherits the label and adds its own name, and both print in order.
-
What does this program print?
class Base { label = "base"; } class Child extends Base { label = "child"; } console.log(new Child().label);Answer
child
The child field replaces the inherited one, which is the silent overwrite the lesson warns about.
-
What does this program print?
type Ctor = new (...args: any[]) => {}; function Tagged<T extends Ctor>(Base: T) { return class extends Base { tag = "card"; }; } function withStamp<T extends Ctor>(Base: T) { return class extends Base { stamped = true; }; } class Card { title = "Draft"; } const One = withStamp(Tagged(Card)); const one = new One(); console.log(one.tag, one.stamped, one.title);Answer
card true Draft
Every layer keeps the members below it, so the tag, the stamp and the title are all there.
-
What does the compiler say about this program?
type Ctor = new (...args: any[]) => {}; function Tagged<T extends Ctor>(Base: T) { return class extends Base { tag: number = "card"; }; } class Base {} const One = Tagged(Base); console.log(new One().tag);Answer
error TS2322: Type 'string' is not assignable to type 'number'.
The field is declared as a number, and the string in the initializer does not fit.
-
What does the compiler say about this program?
class Base {} class Child extends Base { label = "made"; } const one: Base = new Child(); console.log(one.label);Answer
error TS2339: Property 'label' does not exist on type 'Base'.
The variable is typed as the base class, which has no label field, even though the instance does.
Article author: Arthur Isaev