typed-mixins
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

typed-mixins

Mix classes together to share simple pieces of reusable functionality. Just Works™ with TypeScript.

const SuperHero = mixin(Person, [Jumpable, Flyable]);

// hero is automatically typed as Person & Flyable & Jumpable
const hero = new SuperHero();

No casting objects as an interface, no any. typed-mixins work perfectly out of the box with TypeScript.

Installing

npm install typed-mixins

Or

yarn add typed-mixins

Usage

import { mixin } from 'typed-mixins'

class Person {
  name = 'Ordinary Man'
}
class Jumpable {
  static legs = 2;
  jump = () => console.log('jumped');
}
class Flyable {
  static soar() { console.log('soared') }
  height = 12;
}

const SuperHero = mixin(Person, [Jumpable, Flyable]);

// hero is automatically typed as Person & Flyable & Jumpable
const hero = new SuperHero();

// shorthand for all mixed classes
let nextHero: typeof SuperHero.Instance; 

// Use instance properties
console.log(hero.name)   // 'Ordinary Man'
console.log(hero.height) // 12
hero.jump()              // 'jumped'

// Use static properties
SuperHero.soar()         // 'soared'

You can further customize mixed classes:

const SuperHero = mixin(Person, [Jumpable, Flyable]);
class CapedSuperHero extends SuperHero {
  twirl() {
    this.jump()
    console.log('twirled')
  }
}
const capeGuy = new CapedSuperHero();
capeGuy.twirl();         // 'jumped', 'twirled'

Restrictions

  • The base class and all mixins must be classes. No plain-old JavaScript objects.
  • Mixin classes cannot use constructor arguments (prevents really tortured syntax in the returned constructor)
  • This package ships as ES6 because transpiled ES5 classes cannot extend native classes

/typed-mixins/

    Package Sidebar

    Install

    npm i typed-mixins

    Weekly Downloads

    0

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    9.58 kB

    Total Files

    7

    Last publish

    Collaborators

    • kyle-n