K
K
Konstantin2018-07-16 00:08:44
typescript
Konstantin, 2018-07-16 00:08:44

Inheritance and TypeScript interface?

I have the following classes, one parent and two others are descendants:

export class ProfileBase implements IProfile {}

export class StaffProfile extends ProfileBase implements IStaff {}

export class PupilProfile extends ProfileBase implements IPupil {}

There is also a simple factory:
export class ProfileFactory {

  public static set(response: Profile): IProfile {

switch (response.role) {
      case 'Teacher':
        return new StaffProfile(response);
        ....
}

The factory must return a ready-made object with the same interface.
The problem is that I can't access the methods of the ProfileBase base class through the prepared object .
And also, if my factory returned an object of different types that I want to add to an array, what type should this array be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2018-07-16
@Junart1

The problem is that I can't access the methods of the ProfileBase base class through the prepared object.

Of course you can't, in order to be able to, you need to add these methods to the IProfile.
So different types or one type? Now your factory returns objects of the same type (IProfile), but of different classes. Nothing prevents you from making an array from IProfile. Have you read somewhere about polymorphism?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question