Mixins in TypeScript
Compose class behavior from multiple sources using the mixin pattern instead of inheritance.
What you'll learn
- What mixins are and why they solve multiple-inheritance needs
- How to define and apply mixins in TypeScript
- Typing mixin functions with constructor type signatures
- Constrained mixins using generics and extends
Concept
The Mixin Pattern
JavaScript classes only support single inheritance — a class can extend one parent. Mixins are an alternative composition mechanism: you define small, focused classes (mixins) and combine them into a target class. Each mixin adds specific behavior (e.g. Disposable, Serializable, EventEmitter) without a deep inheritance hierarchy. The mixin pattern uses a function that takes a base class and returns an extended class.
Defining and Applying Mixins
A mixin is a function: function Disposable<T extends Constructor>(Base: T) { return class extends Base { dispose() {} } }. You apply mixins by chaining: class MyComponent extends Disposable(Serializable(Base)) {}. The Constructor type (type Constructor<T = {}> = new (...args: any[]) => T) is the standard constraint for mixin base classes. Each mixin adds its methods to the resulting class.
Typing Mixins
Typing mixins correctly is tricky because TypeScript needs to know the combined type. The standard approach uses the Constructor type and generics to preserve the base class's type while adding the mixin's methods. For applied mixins, use interface declarations with extends to merge the types, or use ThisType for more advanced scenarios. Well-typed mixins give you full autocomplete and type checking on the composed class.
Example
// Base constructor type for mixins.
type Constructor<T = {}> = new (...args: any[]) => T;
// Mixin: adds timestamp tracking.
function Timestamped<TBase extends Constructor>(Base: TBase) {
return class extends Base {
createdAt = new Date();
updatedAt = new Date();
touch(): void {
this.updatedAt = new Date();
}
};
}
// Mixin: adds serialization.
function Serializable<TBase extends Constructor>(Base: TBase) {
return class extends Base {
serialize(): string {
return JSON.stringify(this);
}
};
}
// Base class.
class Entity {
constructor(public id: number) {}
}
// Compose: Entity + Timestamped + Serializable.
class Record extends Timestamped(Serializable(Entity)) {}
const rec = new Record(1);
console.log(rec.id); // 1
console.log(rec.createdAt); // Date
rec.touch();
console.log(rec.serialize()); // JSON string
Mixins compose class behavior without multiple inheritance. Each mixin is a function that extends a base class. The Constructor type constrains the base. Chain mixins to combine behaviors: Timestamped(Serializable(Entity)).
Try it
- Code Diff
Compare mixin-based composition with traditional inheritance.
- Code Minifier
Observe how mixin classes compile to JavaScript constructor chains.
Common mistakes
The mistake
Creating deep mixin chains that are hard to debug and type
The fix
Keep mixin chains shallow — two or three levels at most. Deep chains produce confusing type errors and are hard to trace. Prefer composition (has-a) over deep mixin chains (is-a).
The mistake
Forgetting to declare the merged interface for applied mixins
The fix
Applied mixins may need an interface declaration to merge the types for full type checking. Declare 'interface Record extends Timestamped<...>, Serializable<...> {}' to get complete typing.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.