{"name":"ts-mixer","version":"5.0.0-beta.0","description":"A very small TypeScript library that provides tolerable Mixin functionality.","main":"dist/index.js","module":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"tsc","build-clean":"rimraf dist && yarn build","lint":"eslint src/**/*.ts","test":"nyc mocha","coveralls":"cat coverage/lcov.info | coveralls","release":"standard-version"},"devDependencies":{"@commitlint/cli":"^8.3.5","@commitlint/config-conventional":"^8.3.4","@types/chai":"^4.2.8","@types/mocha":"^7.0.1","@types/node":"^13.7.0","@typescript-eslint/parser":"^2.18.0","chai":"^4.2.0","class-validator":"^0.11.0","coveralls":"^3.0.9","eslint":"^6.8.0","husky":"^4.2.1","mocha":"^7.0.1","nyc":"14.1.1","rimraf":"^3.0.1","standard-version":"^7.1.0","ts-node":"^8.6.2","typescript":"^3.7.5"},"homepage":"https://github.com/tannerntannern/ts-mixer#readme","repository":{"type":"git","url":"git+https://github.com/tannerntannern/ts-mixer.git"},"keywords":["typescript","mixin","mixins","multiple inheritance","mixin classes"],"author":{"name":"Tanner Nielsen"},"license":"MIT","readme":"# ts-mixer\r\n[![npm version](https://badgen.net/npm/v/ts-mixer)](https://npmjs.com/package/ts-mixer)\r\n[![Build Status](https://travis-ci.org/tannerntannern/ts-mixer.svg?branch=master)](https://travis-ci.org/tannerntannern/ts-mixer)\r\n[![Coverage Status](https://coveralls.io/repos/github/tannerntannern/ts-mixer/badge.svg?branch=master)](https://coveralls.io/github/tannerntannern/ts-mixer?branch=master)\r\n[![Minified Size](https://badgen.net/bundlephobia/min/ts-mixer)](https://bundlephobia.com/result?p=ts-mixer)\r\n[![Conventional Commits](https://badgen.net/badge/conventional%20commits/1.0.0/yellow)](https://conventionalcommits.org)\r\n\r\n### What is it?\r\n`ts-mixer` is a lightweight package that brings mixins to TypeScript.  Mixins in JavaScript are easy, but TypeScript introduces complications.  `ts-mixer` deals with these complications for you and infers all of the intelligent typing you'd expect, including instance properties, methods, static properties, **generics**, and more.\r\n\r\n[Quick start guide](#quick-start)\r\n\r\n### Why another Mixin implementation? \r\nIt seems that no one has been able to implement TypeScript mixins gracefully.  Mixins as described by the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/mixins.html) are far less than ideal.  Countless online threads feature half-working snippets, each one interesting but lacking in its own way.\r\n\r\nMy fruitless search has led me to believe that there is no perfect solution with the current state of TypeScript.  Instead, I present a \"tolerable\" solution that attempts to take the best from the many different implementations while mitigating their flaws as much as possible.\r\n\r\n## Features\r\n* can mix plain classes\r\n* can mix classes that extend other classes\r\n* can mix abstract classes (with caveats)\r\n* can mix generic classes (with caveats)\r\n* proper constructor argument typing (with caveats)\r\n* proper handling of protected/private properties\r\n* proper handling of static properties\r\n* [multiple options](#settings) for mixing (ES6 proxies vs copying properties)\r\n\r\n#### Caveats\r\n* Mixing abstract classes requires a bit of a hack that may break in future versions of TypeScript.  See [dealing with abstract classes](#dealing-with-abstract-classes) below.\r\n* Mixing generic classes requires a more cumbersome notation, but it's still possible.  See [dealing with generics](#dealing-with-generics) below.\r\n* ES6 made it impossible to use `.apply(...)` on class constructors, which means the only way to mix instance properties is to instantiate all the base classes, then copy the properties over to a new object.  This means that (beyond initializing properties on `this`), constructors cannot have [side-effects](https://en.wikipedia.org/wiki/Side_effect_%28computer_science%29) involving `this`, or you will get unexpected results.  Note that constructors need not be _completey_ side-effect free; just when dealing with `this`.\r\n\r\n## Non-features\r\n* `instanceof` support.  Difficult to implement, and not hard to work around (if even needed at all).\r\n\r\n# Quick Start\r\n## Installation\r\n```\r\n$ npm install ts-mixer\r\n```\r\n\r\nor if you prefer [Yarn](https://yarnpkg.com):\r\n\r\n```\r\n$ yarn add ts-mixer\r\n```\r\n\r\n## Examples\r\n### Minimal Example\r\n```typescript\r\nimport { Mixin } from 'ts-mixer';\r\n\r\nclass Foo {\r\n    protected makeFoo() {\r\n        return 'foo';\r\n    }\r\n}\r\n\r\nclass Bar {\r\n    protected makeBar() {\r\n        return 'bar';\r\n    }\r\n}\r\n\r\nclass FooBar extends Mixin(Foo, Bar) {\r\n    public makeFooBar() {\r\n        return this.makeFoo() + this.makeBar();\r\n    }\r\n}\r\n\r\nconst fooBar = new FooBar();\r\n\r\nconsole.log(fooBar.makeFooBar());  // \"foobar\"\r\n```\r\n\r\n[Play with this example](https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&pln=4&pc=2#code/JYWwDg9gTgLgBAbzgWWAD2AOzgXzgMyghDgHIYBnAWhHQFMpSBuAKBYGMAbAQworgBiECIhZxxcMERh12MgCZwQ3ANZ0hEABQBKURP1wodGAFco2UvmHMxEnC3scefOACFuUPRKkQZcuorKau5QOl4GhsZmFgBGHjb69o5cvPwaIXB0aDKY8vyoGJiaGgA0bh66CLbiYCYxnMDsSqrqwiFhVRHiRqbmcDAAFsAUAHRBrVq6ANT9Q6Pj7dqsiQ5s7BCYFPBWEBkAvHCYdADugm0eOqwcGxQQnHQjnBAA5po7IWMt6RfaS+IA9P84AAiHZxKDAoA)\r\n\r\n### Mixing Abstract Classes\r\nAbstract classes, by definition, cannot be constructed, which means they cannot take on the type, `new(...args) => any`, and by extension, are incompatible with `ts-mixer`.  BUT, you can \"trick\" TypeScript into giving you all the benefits of an abstract class without making it technically abstract.  The trick is just some strategic `// @ts-ignore`'s:\r\n\r\n```typescript\r\nimport { Mixin } from 'ts-mixer';\r\n\r\n// note that Foo is not marked as an abstract class\r\nclass Foo {\r\n    // @ts-ignore: \"Abstract methods can only appear within an abstract class\"\r\n    public abstract makeFoo(): string;\r\n}\r\n\r\nclass Bar {\r\n    public makeBar() {\r\n        return 'bar';\r\n    }\r\n}\r\n\r\nclass FooBar extends Mixin(Foo, Bar) {\r\n    // we still get all the benefits of abstract classes here, because TypeScript\r\n    // will still complain if this method isn't implemented\r\n    public makeFoo() {\r\n        return 'foo';\r\n    }\r\n}\r\n```\r\n\r\n[Play with this example](https://www.typescriptlang.org/play/index.html#code/JYWwDg9gTgLgBAbzgWWAD2AOzgXzgMyghDgHIYBnAWhHQFMpSBuAKBYHp25MIY64YACwCG8AGIQIcYBW684IYVADWdACZxhs4dmEAjCjCjCAxvBMAbLRRaXrcCVIQs4ruJzgABSlWABzHig6AC44ACIAQQMjU3gQOiEINVkTHTgITAsAT00wMDolOAB3YCEsTV1o4zM4OwoKMJc3MABXPQtgE00q2IVhVUcACgBKUMMoLD9WHDY62QAhQuc3OFb2zr7VRagRxCaV1yCYFqhsUj0lZn3cFhnbK3qHSW24OjQ+TGSUdCxBxwAaODbYZ7FYeIr8QzACwWOB+BKaGECQT8PR0TB0fClWQQfDdca9OZ0WQooKAtGpFoUfgAFSy+QAyiYJmAYNdwdDYVCkSZiGArOVgHiyrJ4okNDJMORpOALHR4pg+GprmsOl1FANJLtlgdDgkTmd8JIrisZjMgA)\r\n\r\nDo note that while this does work quite well, it is a bit of a hack and I can't promise that it will continue to work in future TypeScript versions.\r\n\r\n### Mixing Generic Classes\r\nFrustratingly, it is _impossible_ for generic parameters to be referenced in base class expressions.  No matter how you try to slice it, you will eventually run into `Base class expressions cannot reference class type parameters.`\r\n\r\nThe way to get around this is to leverage [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html), and a slightly different mixing function from ts-mixer: `mix`.  It works exactly like `Mixin`, except it's a decorator, which means it doesn't affect the type information of the class being decorated.  See it in action below:\r\n\r\n```typescript\r\nimport { mix } from 'ts-mixer';\r\n\r\nclass Foo<T> {\r\n    public fooMethod(input: T): T {\r\n        return input;\r\n    }\r\n}\r\n\r\nclass Bar<T> {\r\n    public barMethod(input: T): T {\r\n        return input;\r\n    }\r\n}\r\n\r\ninterface FooBar<T1, T2> extends Foo<T1>, Bar<T2> { }\r\n@mix(Foo, Bar)\r\nclass FooBar<T1, T2> {\r\n    public fooBarMethod(input1: T1, input2: T2) {\r\n        return [this.fooMethod(input1), this.barMethod(input2)];\r\n    }\r\n}\r\n```\r\n\r\n[Play with this example](https://www.typescriptlang.org/play/index.html?experimentalDecorators=true&ssl=1&ssc=1&pln=22&pc=1#code/JYWwDg9gTgLgBAbziYAPOBfOAzKERwDkMAzgLQqoCmUhA3AFAMDGANgIYklwBiEEAHgAqAPkQM4kuGACuAI1bBmOfgFkqMABYQAJgApgAO1kwAXHCEBKc0PFT7cKBplRDcIycb2MDHyw5ccABC7FDCYggSUrIKSnByoepaugbGMmYW1hZ2DpJOMC5uHuleUj5+RjA02OzMVLz8IWFCAIwANBYATGJUqFWGOtx8gq0iHU3C3YiYDAAClHrD46GW-pxDjaHC7V0RUZIxisrYm1BJ2vrFMC02O1edNp2WObn5hXAA2lrAJAB0JxBzikri1LB1vn8EmcNBdUiYngBdUqScoMIA)\r\n\r\nKey takeaways from this example:\r\n* `interface FooBar<T1, T2> extends Foo<T1>, Bar<T2> { }` makes sure `FooBar` has the typing we want, thanks to declaration merging\r\n* `@mix(Foo, Bar)` wires things up \"on the JavaScript side\", since the interface declaration has nothing to do with runtime behavior.\r\n* The reason we have to use the `mix` decorator is that the typing produced by `Mixin(Foo, Bar)` would conflict with the typing of the interface.  `mix` has no effect \"on the TypeScript side,\" thus avoiding type conflicts.\r\n\r\n## Settings\r\nts-mixer has multiple strategies for mixing classes which can be configured by modifying `Settings` from ts-mixer.  For example:\r\n\r\n```typescript\r\nimport { Settings, Mixin } from 'ts-mixer';\r\n\r\nSettings.prototypeStrategy = 'proxy';\r\n\r\n// then use `Mixin` as normal...\r\n```\r\n\r\n### `Settings.prototypeStrategy`\r\n* Determines how ts-mixer will mix class prototypes together\r\n* Possible values:\r\n    - `'copy'` (default) - Copies all methods from the classes being mixed into a new prototype object.  (This will include all methods up the prototype chains as well.)  This is the default for ES5 compatibility, but it has the downside of stale references.  For example, if you mix `Foo` and `Bar` to make `FooBar`, then redefine a method on `Foo`, `FooBar` will not have the latest methods from `Foo`.  If this is not a concern for you, `'copy'` is the best value for this setting.\r\n    - `'proxy'` - Uses an ES6 Proxy to \"soft mix\" prototypes.  Unlike `'copy'`, updates to the base classes _will_ be reflected in the mixed class, which may be desirable.  The downside is that method access is not as performant, nor is it ES5 compatible.\r\n\r\n### `Settings.staticsStrategy`\r\n* Determines how static properties are inherited\r\n* Possible values:\r\n    - `'copy'` (default) - Simply copies all properties (minus `prototype`) from the base classes/constructor functions onto the mixed class.  Like `Settings.prototypeStrategy = 'copy'`, this strategy also suffers from stale references, but shouldn't be a concern if you don't redefine static methods after mixing.\r\n    - `'proxy'` - Similar to `Settings.prototypeStrategy`, proxy's static method access to base classes.  Has the same benefits/downsides.\r\n\r\n# Author\r\nTanner Nielsen <tannerntannern@gmail.com>\r\n* Website - [tannernielsen.com](http://tannernielsen.com)\r\n* Github - [tannerntannern](https://github.com/tannerntannern)\r\n","readmeFilename":"README.md","gitHead":"1ff40359a6295aa9e43d703cbcf3429caa340e88","bugs":{"url":"https://github.com/tannerntannern/ts-mixer/issues"},"_id":"ts-mixer@5.0.0-beta.0","_nodeVersion":"10.17.0","_npmVersion":"6.7.0","dist":{"integrity":"sha512-gFWq4T78NxOd2QymAtAmLx1T1Gx4wikIqkrzBvm8yDjAbWh1uekcgfrBbcJqHUVEpAh/vvdktRe0608Cb3JWng==","shasum":"bd0911dde0ac2174dbe4d23c26a0b4444494186f","tarball":"http://registry-npm.ywwl.com/ts-mixer/download/ts-mixer-5.0.0-beta.0.tgz","fileCount":16,"unpackedSize":30225,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeNluICRA9TVsSAnZWagAAs5YP/iVdgdISy+/N4G7mCuGy\nD8MBzFFmgVuz5OLVztPM2/8bgm1MB4kjl7ygWOIoGo6PTipUD/dXhiM48pQV\nBW0SCVleDzVA5KshCv9+TZd7x3HK95KKw8OJzeO4uqKTbYQA8MqrBMM81VXt\nD/qMllys+nm+IALVewWCK74DyNh5UsZY/40CgwmUj/5oP7ZGv4GqVUWhCAeN\nCdN868FbPO0T98b5f7+wMvgK/balms+mydVr5aHDaDmkcG/p6b40hW/1IEo2\nXS2DlD+aoi4bKg4qABGnmANLbumB7AMsp1eB8Br47+XXz/BBL9BWWF8kRhbK\nZQ2x62KS2A7DSvkWjOUYs0AJauwW2B0MRoqcoS4zD8sR99y5aHbghoN9y8Ts\n0dSMq9QnJO/7taTi/YDmNw3f5e6votPE15X1fu6A8w5p0+710ic0TaDZvYlI\nHFzCiJsFlgpY31YZs5ylKyGISIAbWjiQ8of666psFvL4ZoOc/tTZE6kUDfi6\nKprYmCmRiNCbAiQQwkdpEkqwnhgq89A5OnnetanB1KS+n6K992oQmR79AY+H\nfiGBgj3SC8M+D9SeSpdFyGjXWipzp29POgY976b8jm3Qto9AsnI0DtA+GWUZ\nLR/zyVKMAv4QiC1BmBrTSmIHBnvF4GCTLDT4w31uZ76x9PfH8SWQOOHlqqsC\nBJyO\r\n=wuL5\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICZptS2KfEPF8j23dc6C+5evT927kf4vqahexzQYXmv/AiBVY6tmLMa97RMQd0K3DrOT+nrLzyvir0xcbKTqLAtCjQ=="}],"size":10064,"noattachment":false,"key":"/ts-mixer/-/ts-mixer-5.0.0-beta.0.tgz"},"maintainers":[{"name":"tannerntannern","email":"tannerntannern@gmail.com"}],"_npmUser":{"name":"tannerntannern","email":"tannerntannern@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ts-mixer_5.0.0-beta.0_1580620679548_0.7052561823371701"},"_hasShrinkwrap":false,"publish_time":1580620679709,"_cnpm_publish_time":1580620679709,"dist-tags":{"beta":"6.0.0-beta.0","latest":"6.0.4"}}