由于编程和魔术的作用,我的编辑器(vscode)知道如果我让Typescript为我键入内容,则返回querySelectorAll
是哪种类型。
但是手动在变量compiled
下描述类型,对我来说太复杂了。
如何明确描述其类型?
const fixture = TestBed.createComponent(Proxy);
const component = fixture.componentInstance;
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement; // Magic does its work and type is known.
const titleElement = compiled.querySelectorAll('h3'); // Hovering pointer over method returns the type.
expect(titleElement.innerText).toBe('a title');
但是由于种种原因(茉莉花/角使用forEach
设置测试),我必须事先声明compiled
。目前,我声明了let compiled: any;
,这使Typescript失去了所有类型知识。
let compiled: any; // <- this is the variable I want to type.
const fixture = TestBed.createComponent(Proxy);
const component = fixture.componentInstance;
fixture.detectChanges();
compiled = fixture.debugElement.nativeElement;
const titleElement = compiled.querySelectorAll('h3')[0]; // Hovering does not yield type.
expect(titleElement.innerText).toBe('a title');