Flow: HTMLElement type is incompatible with HTMLCanvasElement

Created on 13 Dec 2014  路  2Comments  路  Source: facebook/flow

Try this:

var canvas: HTMLCanvasElement;
canvas = document.createElement("canvas");

Blows up with

/private/var/folders/07/jtkf72yn4_587wv8zc7ntg6h0000gn/T/flow_bjorn/flowlib_282271de/lib/dom.js:301:1,316:1: HTMLElement
This type is incompatible with
  /private/var/folders/07/jtkf72yn4_587wv8zc7ntg6h0000gn/T/flow_bjorn/flowlib_282271de/lib/dom.js:326:1,332:1: HTMLCanvasElement

Doesn't seem right. If I make it just an HTMLElement it blows up when I try to use the getContext method on canvas.

Most helpful comment

document.createElement returns an HTMLElement.

Imagine some code like:
var x = document.createElement(someInputFromuser);
And you can see why Flow can't know at check time which type of DOM element x is.

var canvas = document.createElement("canvas");
if (!(canvas instanceof HTMLCanvasElement)) {
  return;
}
canvas.getContext();  // works here

All 2 comments

document.createElement returns an HTMLElement.

Imagine some code like:
var x = document.createElement(someInputFromuser);
And you can see why Flow can't know at check time which type of DOM element x is.

var canvas = document.createElement("canvas");
if (!(canvas instanceof HTMLCanvasElement)) {
  return;
}
canvas.getContext();  // works here

Ok got it, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Macil picture Macil  路  186Comments

danvk picture danvk  路  73Comments

Gozala picture Gozala  路  54Comments

StoneCypher picture StoneCypher  路  253Comments

STRML picture STRML  路  48Comments