My answer follows what you have written:
All classes inherit from one Component class. This function in a certain way searches for objects that are instances of these classes.
So in the solution I am using some base class Component and to search for some subclass members SubComponent.
// some empty classes
class Component {}
class SubComponent extends Component {}
function findObjects(...args: Component[]): SubComponent[] {
return args.filter(el => el instanceof SubComponent); // do some by instanceof for example
}
// using
const results = findObjects(new Component(), new Component(), new SubComponent());
// results is an array only with SubComponent element
The definition of the function is following - findObjects(...args: Component[]): SubComponent[].
...args: Component[] - we take as arguments unknown number of Component instances
: SubComponent[] - we return array of SubComponents
I would propose to change little bit definition of this, as we return array, then better would be to take also array:
function findObjects(arr: Component[]): SubComponent[] {
return arr.filter(el => el instanceof SubComponent); // do some instanceof for example
}
// using
const results = findObjects([new Component(), new Component(), new SubComponent()]);
// results is an array only with SubComponent element
Pay attention that now the argument is one - arr but it is consistent with the output, we put array in, and take array out. Just a suggestion though.