mixin.ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Mixin.ts

A typescript library which allows a class to extend multiple classes.

Import

import { mixin } from "mixin.ts"

Usage

let className = mixin([classToInheritFrom1, classToInheritFrom2,...])

classes can be written as TypeScript classes or just functions

// function
function Car () {}
Car.prototype.drive = (name: string) => {
    console.log(`This ${name} can drive very fast`);
}
Car.prototype.model = "Toyota";
 
 
// TypeScript class
class Lorry {
    carry(weight:number) {
        console.log(`This vehicle can carry ${weight} kg`);
    }
}
 
// Truck contains methods and properites from both Car and lorry
const Truck = mixin([Car,Lorry]);
 
Truck.drive("truck"); // This truck can drive very fast
Truck.carry(10); // This vehicle can carry 10 kg
 
console.log(Truck.model) // Toyota

By default, mixin returns an instance of the created class. To return the created class or function itself, pass true as the second argument to mixin.

const Truck = mixin([Car,Lorry],true);
const truck = new Truck();
truck.drive("new truck"); // This new truck can drive very fast
truck.carry(100); // This vehicle can carry 100 kg

Install Choices

License

(The MIT License)

MIT License

Package Sidebar

Install

npm i mixin.ts

Weekly Downloads

1

Version

1.0.1

License

none

Unpacked Size

6.82 kB

Total Files

8

Last publish

Collaborators

  • alfmo