basarat / basarat/typescript-book
Mixin
- Dominant language
- TypeScript
- Stars
- 21.6k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
Here is a good solution : https://github.com/Microsoft/TypeScript/issues/6502#issuecomment-173312747 that uses `Object.assign`
https://github.com/Microsoft/TypeScript/issues/2919#issuecomment-173384825
``` ts
function Mixin(...mixins) : new() => T {
class X {
constructor() {
mixins[0].call(this);
}
}
Object.assign(X.prototype, ...mixins.map(m => m.prototype));
return X;
}
abstract class Dog {
bark() { console.log('bark!'); }
}
abstract class Duck {
quack() { console.log('quack!'); }
}
class Base {
constructor() {
console.log('base ctor');
}
world = "world";
hello() {
console.log(`hello ${this.world}`);
}
}
interface DoggyDuck extends Base, Dog, Duck { }
class Mutant extends Mixin(Base, Dog, Duck) {
}
let xavier = new Mutant();
xavier.bark();
xavier.quack();
xavier.hello();
```
Contributor guide
Research direction
Start with the two linked TypeScript discussions and compare their Object.assign mixin approach with the TypeScript book’s existing coverage. Done means documenting a verified Mixin pattern based on the shown example, including its constructor and inherited methods.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100