Code Monkey home page Code Monkey logo

typed-trait's Introduction

Magical-Mixin Build Status npm mit ts

The only 100% typesafe class mixin for TypeScript

Features

  • Complete compile (as you type) time safety for class mixins: Know at compile time which methods are available for your mixed in class, without the boilerplate that the TypeScript docs's approach suffers from.
  • Compile time error when multiple mixins implement the same method or property: A common shortcoming of mixins is undefined behavior when multiple mixins implement the same property or method. In some languages the last mixins wins, in some the first one wins, in some you need to explicitly override methods, and in some overrides are forbidden. Magical-mixin takes the last approach for maximum safety and predictability.
  • Simple, elegant syntax: Just extends mixin(ClassA, ClassB).

Installation

# Using Yarn:
yarn add magical-mixin

# Or, using NPM:
npm install magical-mixin --save

Usage

import { mixin } from 'magical-mixin'

class A {
  a() {}
}
class B {
  b() {}
}
class Bad {
  a() {}
  c() {}
}

class MyClass extends mixin(A, B) {}
let c = new MyClass
c.a() // OK
c.b() // OK

class MyBadClass extends mixin(A, Bad) {}
// Compile Error: Type '"Error: Multiple mixins implement the following methods:" & Methods<"a">' is not a constructor function type.

class MyOtherBadClass extends mixin(A, B, Bad) {}
// Compile Error: Type '"Error: Multiple mixins implement the following methods:" & Methods<"a">' is not a constructor function type.

TODO

  • Add support for calling mixin() with more than 3 parameters
  • Define behavior for constructors
  • Don't export Methods utility class

Tests

yarn test

License

MIT

typed-trait's People

Contributors

bcherny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

typed-trait's Issues

What is super?

First of all, amazing work! This solves a real problem.

Consider this:

class A {
        constructor(private foo = () => 'A') {

        }
}

class B {
        constructor(private foo = () => 'B') {}
}

class C extends mixin(A, B) {}

TypeScript is already complaining:

    Property 'foo' has conflicting declarations and is inaccessible in type 'C'.

I'm confused what would be super when you extending mixins

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.