or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdcore-mixing.mddecorators.mdgeneric-classes.mdindex.mdmixin-detection.md
tile.json

tessl/npm-ts-mixer

A TypeScript library that provides tolerable Mixin functionality with multiple inheritance support.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/ts-mixer@6.0.x

To install, run

npx @tessl/cli install tessl/npm-ts-mixer@6.0.0

index.mddocs/

ts-mixer

ts-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.

Package Information

  • Package Name: ts-mixer
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install ts-mixer

Core Imports

import { Mixin, mix, hasMixin, decorate, settings } from "ts-mixer";

For CommonJS:

const { Mixin, mix, hasMixin, decorate, settings } = require("ts-mixer");

Basic Usage

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"

Architecture

ts-mixer is built around several key components:

  • Mixin Function: Core function that creates mixed classes using prototype copying or ES6 proxies
  • Decorator System: Support for preserving and inheriting decorators through mixing
  • Type Safety: Full TypeScript support with complex generic type inference for up to 10 classes
  • Multiple Strategies: Configurable strategies for prototype and static property handling
  • Mixin Tracking: Internal system for tracking mixin relationships and providing instanceof replacement
  • Constructor Handling: Sophisticated handling of constructor parameters and init functions

Capabilities

Core Mixing

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 classes

Core Mixing

Generic Class Support

Specialized mixing support for generic classes using the decorator pattern.

function mix(...ingredients: Class[]): (decoratedClass: any) => any;

Generic Classes

Mixin Detection

Type-safe mixin detection and type narrowing functionality.

function hasMixin<M>(
  instance: any,
  mixin: abstract new (...args) => M
): instance is M;

Mixin Detection

Decorator Support

Decorator preservation and inheritance system for mixed classes.

function decorate<T extends ClassDecorator | PropertyDecorator | MethodDecorator>(
  decorator: T
): T;

Decorators

Configuration

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;

Configuration

Types

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 */;