https://github.com/bakerface/ts-mixin
A type-only package for creating mixins in TypeScript
https://github.com/bakerface/ts-mixin
Last synced: 3 months ago
JSON representation
A type-only package for creating mixins in TypeScript
- Host: GitHub
- URL: https://github.com/bakerface/ts-mixin
- Owner: bakerface
- Created: 2021-04-04T16:00:28.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-04T16:25:35.000Z (about 5 years ago)
- Last Synced: 2025-09-01T08:02:53.122Z (8 months ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ts-mixin
**A type-only package for creating mixins in TypeScript**
``` typescript
import type { Mixin } from "@bakerface/ts-mixin";
interface AmountFeatures {
getAmount(): number;
setAmount(n: number): void;
}
interface AddFeatures {
add(a: number): void;
}
const withAdd: Mixin = (Base) =>
class extends Base implements AddFeatures {
add(n: number): void {
this.setAmount(this.getAmount() + n);
}
};
interface SubtractFeatures {
subtract(n: number): void;
}
const withSubtract: Mixin = (Base) =>
class extends Base implements SubtractFeatures {
subtract(n: number): void {
return this.add(-n);
}
};
class Amount implements AmountFeatures {
constructor(private amount: number) {}
getAmount(): number {
return this.amount;
}
setAmount(n: number): void {
this.amount = n;
}
}
const Calculator = withSubtract(withAdd(Amount));
const calculator = new Calculator(0);
calculator.add(5);
calculator.subtract(3);
calculator.getAmount(); // 2
```