Flow HTMLElement.querySelector返回iframe

tuf*_*uff 3 javascript flowtype

Flow似乎没有认识到querySelector可能会返回以下子类型HTMLElement:

var myIframe = document.querySelector('iframe');

function foo(iframe: HTMLIFrameElement): void {
  // I want to do iframe stuff!
}

foo(myIframe);
Run Code Online (Sandbox Code Playgroud)

产生

10: foo(myIframe);
        ^ HTMLElement. This type is incompatible with
6: function foo(iframe: HTMLIFrameElement): void {
                        ^ HTMLIFrameElement
Run Code Online (Sandbox Code Playgroud)

https://flowtype.org/try上.

除了键入它之外,有什么方法可以myIframe让我使用它的HTMLElement属性和HTMLIFrameElement属性Object吗?

小智 8

Flow不知道如何解析选择器,它需要做什么来理解将返回什么类型的元素.这是能够理解getElementsByTagName的简单的API,虽然如此,它知道getElementsByTagName('iframe')回报HTMLCollection<HTMLIFrameElement>.

使用querySelector,你需要施展它.像这样的东西:

var myIframe = ((document.querySelector('iframe'): any): HTMLIFrameElement);