tsmixin
Simple mixin for TypeScript
install
npm i tsmixin
motivation
TypeScript Handbook entry on mixins describes an approach thal allows for mixins, but is a bit tedious to code: forces you to add class interface override, apply the mixins separately, and requires to add a helper function to your code base.
If you, like me, wanted it as simple as class X extends mixin(X, Y, Z) { ...
then this package might be of use.
Under the hood
The mixin()
or extmixin()
functions create and return anonymous class, with the mixin properties added.
Example #1: mixin()
E.g. mixin with no constructor/super call
; // Mixin class #2 ;my.call;my.activate;
Example #2: extmixin()
E.g. extend the first argument class and mix in the rest
; // Mixin class #2 ; // notice that it will require the constructor arguments of Callablemy.call;my.activate;console.logmy.aNumber; // 1
Notes
- extmixin() can not use an abstract class as the extnded class, because of this intentional TypeScript limitation