querySelectorAll的打字稿类型

时间:2020-05-15 07:44:47

标签: angular typescript jasmine

由于编程和魔术的作用,我的编辑器(vscode)知道如果我让Typescript为我键入内容,则返回querySelectorAll是哪种类型。

但是手动在变量compiled下描述类型,对我来说太复杂了。
如何明确描述其类型?


jasmine / angular中,我可以(缩写):

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');

0 个答案:

没有答案