0

I have an application with the following types setup and in use.

type CompanyFounder {
    name: string;   
    age: number;
    salary: number;
    sharesHeld: number;
    getReasonStartedCompany: string;
};

type NonExecDirector = {
    name: string;   
    age: number;
    sharesHeld: number;
    string[]: getExternalStuff;
};

I was tasked with adding a new type:

type Person = {
    name: string;   
    age: number;
};

I figured I could improve the code and reduce duplicating fields on related types using the interface segregation principle. Here is what I came up with:

type Person = {
    name: string;   
    age: number;
};

interface Employee extends Person {
    salary: number;
}

interface Director extends Person {
    sharesHeld: number;
}

interface CompanyFounder extends Director, Employee {
    getReasonStartedCompany: string;
}

interface NonExecDirector extends Director {
    string[]: getExternalStuff;
}

However, I think I have a problem. The whole system works as it did before the change but I just want to check if there is actually a way around the fact that the Company founder is receiving name and age twice because it extends both Director and Employee.

Just to get people from the server-side/backend devs involved, here is the same problem in a C# example

public interface Person {
    string GetName();   
    int GetAge();
}

public interface Employee : Person {
    int GetSalary();
}

public interface Director : Person {
    int GetSharesHeld();
}

public interface CompanyFounder : Director, Employee {
    string GetReasonStartedCompany();
}

public interface NonExecDirector : Director {
    string[] GetExternalStuff();
}

So, problem is that the Company founder is receiving GetName() and GetAge() twice because it implements both Director and Employee.

Can anyone help with this problem?

Andy
  • 2,124
  • 1
  • 26
  • 29
  • 1
    Type/interface is just a contract, it doesn't implement anything. There's also no duplication here.. – Aleksey L. Nov 18 '21 at 19:47
  • 1
    I suspected it was a lack in my understanding. Thank you so much. – Andy Nov 18 '21 at 19:48
  • 1
    Actually, it is a complicated issue. Let's say that the company founder is known as _Bill_ to the employees but prefers _William_ when the name is shown in the director listing. It might be nice to have explicit implementations of `Employee.GetName` and `Director.GetName`. C# allows explicit interface implementations, but it won't allow explicit implementations of interfaces one back in the inheritance chain. If, for example, both employee and director had `GetPicture` declared, then a class could implement `Employee.GetPicture` and `Director.GetPicture`, but not `Employee.GetAge` – Flydog57 Nov 18 '21 at 19:56
  • 1
    This, by the way, bit me sometime in the last month - I can't remember what the issue was. The solution was a complete Kludge – Flydog57 Nov 18 '21 at 19:57
  • @Flydog57 I am not sure if explicit interface implementation is supported in TS yet – Nikhil Vartak Dec 09 '21 at 14:28

1 Answers1

1

Why is the director not receiving a salary? Director is an employee which in turn naturally is a person.

public interface Director : Employee {
    int GetSharesHeld();
}

public interface CompanyFounder : Director {
    string GetReasonStartedCompany();
}
Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
  • Because a non executive director extends director but not employee. The interfaces maps the org structure. Like the CompanyFounder , a FinanceDirector would extend Director and Employee. Aside from all this, I have recently been working with Omit and Pick in Typescript and they actually solve the problem in a different non traditional OOP way. – Andy Dec 09 '21 at 17:15