The only 100% typesafe class mixin for TypeScript
- 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)
.
# Using Yarn:
yarn add magical-mixin
# Or, using NPM:
npm install magical-mixin --save
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.
- Add support for calling
mixin()
with more than 3 parameters - Define behavior for constructors
- Don't export
Methods
utility class
yarn test
MIT