将mixins与node.js中的泛型结合

时间:2019-03-31 07:16:12

标签: typescript generics mixins

我正在开发一个库,在该库中我想为相同的库方法支持一些不同的扩展。
这是我简称为mixins的代码,只是为了给您一个想法:

type Creator<T = {}> = new (...args: any[]) => T;
class DefaultCreator {
        world:string[] = [];
        someMagic() {
            this.world.push("magic")
        }
        getWorldList() {
            return this.world;
        }
}

function PlantCreator<TBase extends Creator>(Base: TBase) {
    return class extends Base {
        private world = (<DefaultCreator><any>this).world;
        createPlant() {
            this.world.push("Plant")
        }
    };
}

function AnimalCreator<TBase extends Creator>(Base: TBase) {
    return class extends Base {
        private world = (<DefaultCreator><any>this).world;
        createDog() {
            this.world.push("Dog")
        }
    };
}

我正在这样使用它:

const MyWorld = AnimalCreator(PlantCreator(DefaultCreator));
const world = new MyWorld();
world.someMagic();
world.createPlant();
world.createDog();

我的问题是,现在我该如何创建一个从上方获取“ MyWorld”的类?

abstract class Playground {
    abstract createWorld(creator: DefaultCreator);
    play() {
        this.createWorld(new DefaultCreator());
    }
}

我的想法是实现可以使用框架功能(在这里只是玩),并使用定制的Creator(也称为Builder)创建世界。我尝试了泛型,但是无法编译。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

看来这是不可能的。由于MyWorld不是类型,因此它是一个奇怪的类型描述。

当我让我的想法指定类型时,我得到以下一行:

const MyWorld: { new(): ({ world: string[]; createDog(): void } & any); prototype: { world: string[]; createDog(): void } } = AnimalCreator(PlantCreator(DefaultCreator));

我的解决方案是按惯例强迫用户创建用户实例并返回示例的world

我修改了操场,以便获得以下代码:

abstract class Playground {
    abstract createWorld(): string[];
    play() {
        console.log("Creating world containing:");
        this.createWorld().forEach(item => console.log(`- ${item}`))
    }
}

class MyPlayground extends Playground {
    createWorld(): string[] {
        const world = new MyWorld();
        world.someMagic();
        world.createPlant();
        world.createDog();
        return world.getWorldList();
    }
}

new MyPlayground().play();

上面代码的输出:

  

包含以下内容的创造世界:
  -魔术
  -植物
  -狗

答案 1 :(得分:1)

您实际上相距不远,但是模糊了类型和值之间的界限。幸运的是,使用Javascript / Typescript可以做到这一点。

// Match only a constructor
type Creator<T = {}> = new (...args: any[]) => T;

// Match an actual class
interface CreatorClass<T> {
    new(...args: any[]): T;
}

class DefaultCreator {
        world:string[] = [];
        someMagic() {
            this.world.push("magic")
        }
        getWorldList() {
            return this.world;
        }
}

function PlantCreator<TBase extends Creator>(Base: TBase) {
    return class extends Base {
        private world = (<DefaultCreator><any>this).world;
        createPlant() {
            this.world.push("Plant")
        }
    };
}

function AnimalCreator<TBase extends Creator>(Base: TBase) {
    return class extends Base {
        private world = (<DefaultCreator><any>this).world;
        createDog() {
            this.world.push("Dog")
        }
    };
}

interface IPlantWorld {
    createPlant(): void;
}

interface IAnimalWorld {
  createDog();
}

const MyWorld: CreatorClass<IPlantWorld & IAnimalWorld> = AnimalCreator(PlantCreator(DefaultCreator));

abstract class Playground {
    // I want to have a reference of the class' constructor
    createOtherWorld<T>(creator: Creator<T>) {
        return new creator();
    }

    // I want to reference the class itself
    createWorld<T>(creator: CreatorClass<T>): T {
        return new creator() as T;
    }
    play() {
        this.createWorld(DefaultCreator);
    }
}



class EverythingPlaygroundFactory extends Playground {
    play() {
        // provide the type information
        return this.createWorld<IAnimalWorld & IPlantWorld>(MyWorld);
    }
}

let pg = new EverythingPlaygroundFactory();
let world = pg.createWorld(MyWorld);
world.createPlant();
world.createDog();

pg.createOtherWorld(MyWorld.prototype.constructor);

这可能更符合您的需求。

注意事项:

type Creator<T = {}> = new (... args: any[]) => T可以并且将仅引用任何类的实际构造函数,但绝不会匹配整个类/类对象。永远记住,类/函数是可执行对象。