Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dom definition for canvas #61

Closed
lbolla opened this issue Nov 19, 2014 · 6 comments
Closed

Add dom definition for canvas #61

lbolla opened this issue Nov 19, 2014 · 6 comments
Assignees
Labels
incompleteness Something is missing

Comments

@lbolla
Copy link
Contributor

lbolla commented Nov 19, 2014

document.createElement('canvas') returns a type HTMLElement, which does not define methods like "getContext". There should be a HTMLCanvasElement in dom.js similarly to HTMLImageElement.

@avikchaudhuri
Copy link
Contributor

We'll update the dom definitions to be sensitive to string arguments.

@avikchaudhuri avikchaudhuri added the incompleteness Something is missing label Nov 19, 2014
@avikchaudhuri avikchaudhuri self-assigned this Nov 20, 2014
@rajsite
Copy link

rajsite commented Nov 21, 2014

When performing a createElement('canvas') the type can be known from the string, but how would you annotate getElementById('mycanvas') to type as HTMLCanvasElement instead of HTMLElement?

@lbolla
Copy link
Contributor Author

lbolla commented Nov 22, 2014

What I would like, would be to be able to do something like:

var canvas : HTMLCanvasElement = document.createElement('canvas');
var mycanvas : HTMLCanvasElement = document.getElementById('mycanvas');

In other words, createElement and getElementById would be polimorphic. I don't know if Flow supports such feature.

@agentcooper
Copy link
Contributor

+1

jeffmo added a commit to jeffmo/flow that referenced this issue Jul 31, 2015
@ghost
Copy link

ghost commented Aug 4, 2015

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

@samwgoldman
Copy link
Member

createElement("canvas") does currently return a HTMLCanvasElement, so this issue is resolved.

@lbolla Regarding your comment about getElementById returning a specific HTMLElement subtype—Flow does not do that, and it is unsafe to do. Flow doesn't know what HTML the code will execute against, and therefore doesn't know anything more specific.

This is a great use case for dynamic type tests.

var canvas = document.getElementById("mycanvas");
if (canvas instanceof HTMLCanvasElement) {
  // good to go -- type of canvas is refined this block
} else {
  // error condition -- provide some useful debug info
}

I see this as a good thing. Flow is helping you out and directing you to add error handling to make your code safer.

ghost pushed a commit that referenced this issue Oct 14, 2015
Summary: Fixes #819
See also #61

This _should_ work, but I'm running into another bug. `canvastest.js` produces this error instead of the expected one:

```
canvastest.js:4:11,29: call of method getContext
Function cannot be called on
[LIB] dom.js:649:5,14: HTMLCanvasElement

Found 1 error
```

This is caused by overloading string parameters to `getContext` in `lib/dom.js`. If there's just one declaration, it works as expected.

It looks like something similar is going on for the existing `createElement` declarations. For example, this line:

```
var el = document.createElement('canvas');
```

produces this non-sense error:

```
canvastest.js:2:10,41: call of method createElement
Function cannot be called on
[LIB] dom.js:235:5,17: Document
```

@​samwgoldman thoughts? Is this the same as #583?
Closes #837

Reviewed By: @​svcscm

Differential Revision: D2540791

Pulled By: @gabelevi

fb-gh-sync-id: 70484aabe0d2b78f9ed425493e0c63e4ac6c1ee1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incompleteness Something is missing
Projects
None yet
Development

No branches or pull requests

5 participants