A TypeScript library that provides tolerable Mixin functionality with multiple inheritance support.
npx @tessl/cli install tessl/npm-ts-mixer@6.0.0ts-mixer is a TypeScript library that provides comprehensive mixin functionality, enabling multiple inheritance in TypeScript and JavaScript projects. It supports mixing plain classes, classes that extend other classes, abstract classes, generic classes, and classes with decorators, offering multiple mixing strategies and sophisticated constructor parameter handling.
npm install ts-mixerimport { Mixin, mix, hasMixin, decorate, settings } from "ts-mixer";For CommonJS:
const { Mixin, mix, hasMixin, decorate, settings } = require("ts-mixer");import { Mixin } from "ts-mixer";
class Foo {
protected makeFoo() {
return "foo";
}
}
class Bar {
protected makeBar() {
return "bar";
}
}
class FooBar extends Mixin(Foo, Bar) {
public makeFooBar() {
return this.makeFoo() + this.makeBar();
}
}
const fooBar = new FooBar();
console.log(fooBar.makeFooBar()); // "foobar"ts-mixer is built around several key components:
Primary mixin functionality for creating classes that inherit from multiple base classes.
function Mixin<A extends any[], I1, S1>(
c1: Class<A, I1, S1>
): Class<A, I1, S1>;
function Mixin<
A1 extends any[], I1, S1,
A2 extends any[], I2, S2
>(
c1: Class<A1, I1, S1>,
c2: Class<A2, I2, S2>
): Class<Longest<A1, A2>, I1 & I2, S1 & S2>;
// ... overloads for up to 10 classesSpecialized mixing support for generic classes using the decorator pattern.
function mix(...ingredients: Class[]): (decoratedClass: any) => any;Type-safe mixin detection and type narrowing functionality.
function hasMixin<M>(
instance: any,
mixin: abstract new (...args) => M
): instance is M;Decorator preservation and inheritance system for mixed classes.
function decorate<T extends ClassDecorator | PropertyDecorator | MethodDecorator>(
decorator: T
): T;Global configuration settings for controlling mixin behavior.
interface Settings {
initFunction: string | null;
staticsStrategy: "copy" | "proxy";
prototypeStrategy: "copy" | "proxy";
decoratorInheritance: "deep" | "direct" | "none";
}
declare const settings: Settings;type Class<
CtorArgs extends any[] = any[],
InstanceType = {},
StaticType = {},
IsAbstract = false
> = (abstract new(...args: any[]) => InstanceType) & StaticType;
type Longest<
T1 extends any[],
T2 extends any[] = [],
T3 extends any[] = [],
T4 extends any[] = [],
T5 extends any[] = [],
T6 extends any[] = [],
T7 extends any[] = [],
T8 extends any[] = [],
T9 extends any[] = [],
T10 extends any[] = []
> = /* complex conditional type for longest tuple */;