I'm struggling with TypeScript's type checking system, especially when dealing with a composite that contains elements derived from a common base class. How can I create a function that recursively traverses the hierarchy to find and return the first ancestor of a specific type?
abstract class Employee
{
public Superior: Employee;
/** THIS IS NOT WORKING */
public getSuperiorOfType<T extends Employee>( type: typeof T ): T
{
if (this instanceof T) return this;
else if (this.Superior !== undefined) return this.getSuperiorOfType(type);
}
}
class Manager extends Employee {}
class TeamLead extends Employee {}
class Developer extends Employee {}
let tom = new Manager();
let suzanne = new TeamLead();
let ben = new Developer();
ben.Superior = suzanne;
suzanne.Superior = tom;
let x = ben.getSuperiorOfType(Manager); // x = tom
Any assistance on this matter would be greatly appreciated...