@bakerface/ts-mixin
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

ts-mixin

A type-only package for creating mixins in 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<AddFeatures, AmountFeatures> = (Base) =>
  class extends Base implements AddFeatures {
    add(n: number): void {
      this.setAmount(this.getAmount() + n);
    }
  };

interface SubtractFeatures {
  subtract(n: number): void;
}

const withSubtract: Mixin<SubtractFeatures, AddFeatures> = (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

Readme

Keywords

none

Package Sidebar

Install

npm i @bakerface/ts-mixin

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

1.55 kB

Total Files

3

Last publish

Collaborators

  • bakerface