Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Classes, Enums and Decorators

TypeScript class mixins

Reference for TypeScript mixins: functions that take a class and return an extended class, constrained constructor types, and combining several mixins.

A mixin is a function that takes a class and returns a new class that extends it with extra behavior. Because JavaScript classes allow only one parent, mixins are the usual way to compose several independent capabilities onto one class.

TypeScript supports the pattern through a mixin constructor type, a type that describes "any class whose constructor takes any[] and returns T". The compiler then infers the result as the intersection of the base instance type and the added members.

TypeScript
type Constructor<T = {}> = new (...args: any[]) => T;

function Timestamped<TBase extends Constructor>(Base: TBase) {
  return class extends Base {
    createdAt = 1;
  };
}

Try it

TypeScript Two mixins on one class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Constraining the base class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
type Constructor<T = {}> = new (...args: any[]) => T;
type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T;

function Mixin<TBase extends Constructor<Needed>>(Base: TBase) {
  return class extends Base {
    /* added members */
  };
}
class Final extends Mixin(Parent) {}
Part Description
Constructor<T> Constructor type with a rest any[] parameter. TypeScript requires exactly this shape for mixin classes.
TBase extends Constructor<Needed> Restricts which classes the mixin accepts, and gives this the members of Needed.
Returned class An anonymous or named class expression extending Base.

Edge cases:

  • A mixin class must have a single constructor with a rest parameter of type any[], and it passes its arguments straight to super.
  • Declaration output (declaration: true) may fail for classes returned from mixins with private or protected members (error TS4094).
  • To mix into an abstract base, use abstract new in the constraint.

Usage notes

  • Each application creates a new class at runtime, so apply mixins once at module level, not inside functions called repeatedly.
  • Mixin order matters: later mixins wrap earlier ones, and members with the same name in the outer class win.
  • Constructor parameters of the base are preserved because the mixin accepts any[]; you lose parameter checking for the intermediate class but the final class keeps its own signature.
  • Mixins give composition without deep hierarchies, but errors in the type display can be hard to read. Consider plain composition (a field holding a helper object) when the mixin adds only a few methods.
  • Static members of the base are inherited by the result like in ordinary subclassing.
  • instanceof works for the base class and for the classes the mixin returns, but not for the mixin function itself.