Angular can Query subComponents by Types, which is used in Testing like this:
fixture.debugElement.query( By.directive( ComponentType ) );
Now i wanted to make a function which does this for me:
import { ComponentFixture } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Type } from '@angular/core';
export function findSubComponent<T>( fixture: ComponentFixture<any>, componentType: T & Type<any> ): T {
const subComponentDebugElement = fixture.debugElement.query( By.directive( componentType ) );
return subComponentDebugElement.componentInstance;
}
Now here comes the problem. My function currently returns typeof ComponentType
instead of an actual object of ComponentType
and therefore i can not access its properties.
The Type of subComponentDebugElement.componentInstance
here is any, so i can just declare the type in the return Type argument (function (): T)
InstanceType<T>
but since I can't easily use your example in a standalone IDE like the TypeScript Playground (link here) then I can't check. β PurveyorInstanceType<T>
Type 'T' does not satisfy the constraint 'abstract new (...args: any) => any'.
. any clue on how i can define T further to fullfill those constraints? β MercantilismfindSubComponent<T extends abstract new (...args: any) => any>(...
but I still can't test it so π€·ββοΈ β Purveyorexport function findSubComponent<T extends abstract new (...args: any) => any>( fixture: ComponentFixture<any>, componentType: (T & Type<any>) ): InstanceType<T> {
fixed the issue. If you want to post an answer i will accecpt it, otherwise i can also post it myself later, but i will leave you the chance to do it, thanks alot, kind stranger! β Mercantilism