After working with a list of components, an AnyComponent type, and a function to locate a specific component, I encountered an issue:
const components = [Comp1, Comp2, Comp3 ...];
type AnyComponent = typeof components[number];
findComponent(id: string): AnyComponent|undefined {
return components.find(comp => comp.id === id);
}
The dilemma arises from the fact that the findComponent function returns a type like this:
typeof Comp1 | typeof Comp2 | typeof Comp3
Unfortunately, the subsequent function I need to utilize (angulars resolveComponentFactory) necessitates it to be a Type<AnyComponent>
or
Type<typeof Comp1 | typeof Comp2 | typeof Comp3>
Trying to set the return value of findComponent to Type<AnyComponent>
results in an error indicating it's not assignable.
However, altering AnyComponent to this resolves the issue:
type AnyComponent = Comp1 | Comp2 | Comp3;
This modification allows me to assign the findComponent return value to Type<AnyComponent>
, and everything functions as intended.
The drawback of using a union to make everything work is that I end up declaring each component twice, contrary to my goal of defining them only once.
Is there a way to have findComponent return Type<AnyComponent>
without having to duplicate component declarations?