The only 100% typesafe class mixin for TypeScript
override
methods, and in some overrides are forbidden. Magical-mixin takes the last approach for maximum safety and predictability.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.
mixin()
with more than 3 parametersMethods
utility classyarn test
MIT