Closed
Description
- VSCode Version: Code 1.14.2 (cb82feb, 2017-07-19T23:26:08.116Z)
- OS Version: Linux x64 4.10.0-27-generic
I've asked the same question on StackOverflow but I'm rephrasing it here. I'm worried that the answer to "how do I do this?" will wind up being "oh crap, you can't, we didn't think of that."
- Enable type checking on Javascript files (
compilerOptions.checkJs=true
injsconfig.json
) - Write the following code in a
.js
file:
let el = document.querySelector("#definitelyAnImage");
el.src = "image.png"; - Notice that
.src
gets marked as an error -- this is becausequerySelector
returns anElement
but the.src
property is defined on the subclass HTMLImageElement.
In Typescript, I gather the solution would be to use a type-assertion before accessing the subclass property, or use as HTMLImageElement
in the initial assignment. What can I do to get rid of this error in vanilla JS?