ts-mixin-extended
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

semantic-release npm-version

ts-mixin-extended

Extended mixin function for TypeScript. Offers support for chaining mixins extending a common base class or exposing methods not depending on one.

Since TypeScript 2.2 Mix-in classes are supported, but still somewhat unwieldy. This packages aims to simplify chainning mixins while still offering all the Typings support and most of the flexibility of native ts:

// Default approach
class Dog extends Domesticated(Barking(PackHunting(Carnivore(FourLegged(Animal))))) {}

// Using the extended mixin function
class Dog extends mixin(Animal, FourLegged, Carnivore, PackHunting, Barking, Domesticated) {}

Features

  • comprehensible chaining of mixins
  • strong typechecking for mixins with a common base class

Usage

The package exposes one, and only one, function: mixin. It accepts one base class and, all of them standalone or extending the base class:

import mixin, { MixinConstructor } from '../src';

class Positionable {
  public constructor(public x = 0, public y = 0) {}
  public setPosition(x: number, y: number) { this.x = x; this.y = y; }
}

function Walkable<TBase extends MixinConstructor<Positionable>>(Base: TBase) {
  return class Walkable extends Base {
    public forward() { this.x++; }
    public backward() { this.x--; }
  };
}

function Jumpable<TBase extends MixinConstructor<Positionable>>(Base: TBase) {
  return class Jumpable extends Base {
    public jump() { this.y++; }
  };
}

// Person now has the attributes `x` and `y` and the methods:
//   `setPosition`, `forward`, `backward` and `jump`
class Person extends mixin(Positionable, Walkable, Jumpable) {}

For mixins not extending a common base class, an empty anonymous class can be given:

export function Loggable(Base: MixinConstructor) {
  return class Loggable extends Base {
    public log(message: string) { throw new Error(404); }
  };
}
export function Screamable(Base: MixinConstructor) {
  return class Screamable extends Base {
    public scream(message: string) { console.log(message.slice(0, 140).toUpperCase()); }
  };
}

class TheInternet extends mixin(class {}, Loggable, Screamable) {}

License

MIT

Package Sidebar

Install

npm i ts-mixin-extended

Weekly Downloads

165

Version

1.1.3

License

MIT

Unpacked Size

21.4 kB

Total Files

12

Last publish

Collaborators

  • 1nvitr0