Socket
Socket
Sign inDemoInstall

@bakerface/ts-mixin

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bakerface/ts-mixin

A type-only package for creating mixins in TypeScript


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 04 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc