/*保证typescript可以正确计算类型*/export function extend(target: Type , source1: Type ): Type ;export function extend ( target: Type , source1: Type , source2: Type ): Type ;export function extend ( target: Type , source1: Type , source2: Type , source3: Type ): Type ;export function extend ( target: Type , source1: Type , source2: Type , source3: Type , source4: Type ): Type ;/*结束*//*功能*/export function extend(target: Type , ...sources: Type []): Type { return extend2(target)(...sources.reverse());}function extend2(target: Type ) { return (...sources: Type []): Type => { let targetPrototype = target.prototype; // 继承 (function() { // 创建一个干净的实例 function beget() { var F = function() { // sources属性处理 sources.map(source => source.call(this)); }; // sources处理 assign(F.prototype, ...sources.map(source => source.prototype)); return new F(); } let prototype = beget(); prototype.constructor = target; // 继承 target.prototype = prototype; // 原来的方法 assign(target.prototype, targetPrototype); })(); return target as any; };}/*结束*/复制代码
测试
import { expect } from "chai";import { extend } from "./extend";export class A { title: string = "a"; run() { return "a"; }}export class B { title: string = "b"; demo: string = "demo"; add() { return "b"; }}export class D { title: string = "d"; console() { return "d"; }}let C = extend(A, B, D);const c = new C();describe("extend", () => { it("c.demo", () => { expect(c.demo).equal("demo"); }); it("c.add", () => { expect(c.add()).equal("b"); }); it("c.add", () => { expect(c.run()).equal("a"); }); it("c.add", () => { expect(c.console()).equal("d"); });});复制代码