POV
POV

Reputation: 12025

How extend class by another class?

I have tree classes:

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;

}

I need that class EduOrganizationModel will be extended by EduUnitModel and ClassificatorOrganizationsModel.

As result I need to get class EduOrganizationModel with all properties including children.

So, I can not do this:

class EduOrganizationModel extends EduUnitModel, ClassificatorOrganizationsModel {
}

How to solve it?

Upvotes: 0

Views: 324

Answers (1)

Aleksandr Yatsenko
Aleksandr Yatsenko

Reputation: 869

You can use Mixins https://www.typescriptlang.org/docs/handbook/mixins.html for multiple inheritance.

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));
        });
    });
}

Upvotes: 1

Related Questions