mix-town
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Purpose

A nicer way to use mixins that doesn't look like Clojure.

Installation

npm install mix-town

Usage

TypeScript

// Import the library and types
import { mix } from "mix-town"
import type { Constructable } from "mix-town"

// Make some mixins
function Walkable<T extends Constructable> (superclass: T) {
  return class extends superclass {
    canWalk () {
      return true as true
    }
  }
}

function Jumpable<T extends Constructable> (superclass: T) {
  return class extends superclass {
    canJump () {
      return true as true
    }
  }
}

// Apply said mixins.
const base = mix(HTMLElement)
  .with(Walkable)
  .with(Jumpable)
  .ctor

class MyClass1 extends base {}

// Equivalent without the `mix` helper.
class MyClass2 extends Walkable(Jumpable(HTMLElement)) {}

JSDOC

// Import the library and types
import { mix } from "mix-town"

// Make some mixins
/**
 * @template {import("mix-town").Constructable} T
 * @param {T} superclass
 */
function Jumpable(superclass) {
  return class extends superclass {
    canJump () {
      return /** @type {"yes"} */ ("yes")
    }
  }
}

/**
 * @template {import("mix-town").Constructable} T
 * @param {T} superclass
 */
function Walkable (superclass) {
  return class extends superclass {
    canWalk () {
      return /** @type {true} */ (true)
    }
  }
}
const base = mix(HTMLElement)
  .with(Walkable)
  .with(Jumpable)
  .ctor

class MyClass1 extends base {}

// Equivalent without the helper.
class MyClass2 extends Walkable(Jumpable(HTMLElement)) {}

Readme

Keywords

Package Sidebar

Install

npm i mix-town

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

5.34 kB

Total Files

5

Last publish

Collaborators

  • konnor_rogers