Giter VIP home page Giter VIP logo

ts-mixins's Introduction

ts-mixins

Mixins In TypeScript with Code Suggestions.

type Constructor<T> = new(...args: any[]) => T

const isAbstract = Symbol("isAbstract")

export const mixin = <T1 = {}, T2 = {}, T3 = {}, T4 = {}, T5 = {}, T6 = {}, T7 = {}>(
  C1?: Constructor<T1>,
  C2?: Constructor<T2>,
  C3?: Constructor<T3>,
  C4?: Constructor<T4>,
  C5?: Constructor<T5>,
  C6?: Constructor<T6>,
  C7?: Constructor<T7>,
): {
  new(): T1 & T2 & T3 & T4 & T5 & T6 & T7,
} => {
  const baseCtors = [C1, C2, C3, C4, C5, C6, C7]
  class Super {
    constructor() {
      const subClassName = Object.getPrototypeOf(this).constructor.name
      baseCtors.forEach((ctor) => {
        if (!ctor) { return }
        const abstractMethodList = []
        Object.keys(ctor.prototype).forEach((method) => {
          if (ctor.prototype[method][isAbstract]) {
            abstractMethodList.push(method)
          }
        })
        abstractMethodList.forEach((method) => {
          console.assert(!this[method][isAbstract],  `${subClassName} 必须要实现父类 ${ctor.name} 的抽象方法: ${method}`)
        })
      })
    }
  }
  applyMixin(baseCtors, Super)
  return Super as any
}

function applyMixin(baseCtors, Super) {
  baseCtors.forEach((baseCtor) => {
    Object.assign(Super.prototype, baseCtor.prototype)
    const instance = new baseCtor()
    Object.assign(Super.prototype, instance)
  })
}

export const Abstract = () => {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    target[propertyKey][isAbstract] = true
  }
}

ts-mixins's People

Contributors

livoras avatar

Stargazers

Roman avatar allen avatar

Watchers

James Cloos avatar  avatar  avatar

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.