0.1.0 • Published 6 years ago

magical-mixin v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Magical-Mixin Build Status npm mit ts

The only 100% typesafe class mixin for TypeScript

Features

  • Complete compile (as you type) time safety for class mixins: Know at compile time which methods are available for your mixed in class, without the boilerplate that the TypeScript docs's approach suffers from.
  • Compile time error when multiple mixins implement the same method or property: A common shortcoming of mixins is undefined behavior when multiple mixins implement the same property or method. In some languages the last mixins wins, in some the first one wins, in some you need to explicitly override methods, and in some overrides are forbidden. Magical-mixin takes the last approach for maximum safety and predictability.
  • Simple, elegant syntax: Just extends mixin(ClassA, ClassB).

Installation

# Using Yarn:
yarn add magical-mixin

# Or, using NPM:
npm install magical-mixin --save

Usage

import { mixin } from 'magical-mixin'

class A {
  a() {}
}
class B {
  b() {}
}
class Bad {
  a() {}
  c() {}
}

class MyClass extends mixin(A, B) {}
let c = new MyClass
c.a() // OK
c.b() // OK

class MyBadClass extends mixin(A, Bad) {}
// Compile Error: Type '"Error: Multiple mixins implement the following methods:" & Methods<"a">' is not a constructor function type.

class MyOtherBadClass extends mixin(A, B, Bad) {}
// Compile Error: Type '"Error: Multiple mixins implement the following methods:" & Methods<"a">' is not a constructor function type.

TODO

  • Add support for calling mixin() with more than 3 parameters
  • Define behavior for constructors
  • Don't export Methods utility class

Tests

yarn test

License

MIT