Mixin
A mixin is a function that takes a class and returns a new class with extra methods. In TypeScript you type that as a generic constructor in and an intersection (or a subclass) out.
Detailed Explanation
JavaScript has no multiple inheritance. Mixins are the pattern: function Timestamped<TBase extends Constructor>(Base: TBase) { return class extends Base { ... } }. TypeScript can type Constructor and the returned class so that Timestamped(User) is both User and { createdAt: Date }.
The usual helper types:
type Constructor<T = {}> = new (...args: any[]) => T;
The mixin function is <TBase extends Constructor>(Base: TBase) and returns class extends Base. The instance type of the result is InstanceType<TBase> & MixinMembers if you annotate it, or the anonymous class TypeScript infers. Inference of mixin stacks (A(B(C(Base)))) is where the checker struggles. Named intermediate classes help.
Constraints on TBase (TBase extends Constructor<{ id: string }>) let the mixin read this.id. That is a generic constraint on the instance the constructor produces.
Intersection types (User & Timestamped) describe the instance without a real mixin class. Use an intersection when you only need a type. Use a mixin function when you need the methods on the prototype.
Declaration merging does not replace mixins for classes. Merging two interfaces is types only. A mixin adds runtime methods.
The costs: any[] on the constructor args is common and a bit loose; tighter argument tuples are possible and verbose. this inside the mixin class is the intersection, and you sometimes have to annotate it. Mixins that override constructors must call super(...args) and cannot easily add required constructor parameters — pass those through a second function or an init method.
Prefer composition (a helper object you hold) when a mixin stack gets taller than two. Prefer implements plus a shared function when there is no prototype to extend. Use a mixin when several classes need the same methods and you want instanceof / prototype sharing.
JavaScript mixins on plain objects (Object.assign) are a different, weaker pattern; their types are intersections of object types, not class constructors.
Code Examples
1. A typed class mixin.
// 1. A typed class mixin.
type Constructor<T = {}> = new (...args: any[]) => T;
function Timestamped<TBase extends Constructor>(Base: TBase) {
return class extends Base {
createdAt = new Date();
ageMs(): number {
return Date.now() - this.createdAt.getTime();
}
};
}
class User {
constructor(public email: string) {}
}
const TimedUser = Timestamped(User);
const u = new TimedUser("ada@example.com");
console.log(u.email, u.ageMs() >= 0);2. Constraining the base instance.
// 2. Constraining the base instance.
function HasId<TBase extends Constructor<{ id: string }>>(Base: TBase) {
return class extends Base {
label(): string {
return this.id;
}
};
}3. Intersection as the type-only form.
// 3. Intersection as the type-only form.
type WithTimestamps = { createdAt: Date; ageMs(): number };
type Timed = User & WithTimestamps;Common Gotchas
Mixin stacks confuse inference
name the result (`class TimedUser extends Timestamped(User) {}`) if hovers become `typeof (Anonymous)`.
`Constructor` with `any[]` args is loose
a mixin will accept a class whose constructor you then call wrong. Prefer a tighter tuple when you can.
Mixins cannot easily add constructor parameters
use an init method or a factory.