JavaScript inheritance works by having the prototype be created from the super class constructor.
This means if you have something like this:
class A { }
class B extends A { }
class C extends B { }
then you can check for a subclass with .prototype:
console.log(A.prototype instanceof Object);
console.log(B.prototype instanceof Object);
console.log(B.prototype instanceof A);
console.log(C.prototype instanceof A);
console.log(C.prototype instanceof B);
console.log(C.prototype instanceof C); // false
These all log true except the last.
Based on that you can build this function to check if one class is a sub class or the same class as the other:
declare type Class = new (...args: any[]) => any;
function isSubClassOf(cls: Class, superCls: Class): boolean {
return cls === superCls || cls.prototype instanceof superCls;
}
These all log true except the last:
console.log(isSubClassOf(C, A));
console.log(isSubClassOf(C, B));
console.log(isSubClassOf(C, C));
console.log(isSubClassOf(B, A));
console.log(isSubClassOf(A, A));
console.log(isSubClassOf(A, C)); // false
In your case you can then use the function like this:
if (list.findIndex((item) => isSubClassOf(element, item)) === -1) {
list.push(element);
}