如何将班级扩展到另一个班级?

时间:2019-09-13 11:39:18

标签: typescript typescript2.0

我有树类:

class ClassificatorOrganizationsModel {
    protectedcode: string | undefined;
}

class EduUnitModel {
  paren1tId: number | undefined;
  paren2tId: number | undefined;
  paren3tId: number | undefined;
  phone: string | undefined;
}

export class EduOrganizationModel {
  regionId: number | undefined;
  addressId: number | undefined;

}

我需要将EduOrganizationModelEduUnitModel扩展为类ClassificatorOrganizationsModel

结果,我需要获取包含所有属性(包括子级)的类EduOrganizationModel

所以,我不能这样做:

class EduOrganizationModel extends EduUnitModel, ClassificatorOrganizationsModel {
}

如何解决?

1 个答案:

答案 0 :(得分:2)

您可以使用Mixins https://www.typescriptlang.org/docs/handbook/mixins.html 进行多重继承。

interface EduOrganizationModel extends EduUnitModel, ClassificatorOrganizationsModel {}
applyMixins(EduOrganizationModel, [EduUnitModel, ClassificatorOrganizationsModel]);

function applyMixins(derivedCtor: any, baseCtors: any[]) {
    baseCtors.forEach(baseCtor => {
        Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
            Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));
        });
    });
}