I have a typescript variable that stores the result of a DOM query:
let games = document.getElementsByTagname("game");
But what is the correct type of the result array? I would expect an array that contains htmlelements?
// not working
let games : Array<HTMLElement> = document.getElementsByTagName("game");
// also not working
let games : NodeList<HTMLElement> = document.getElementsByTagName("game");
You need to use NodeListOf<T>
, for example:
let games : NodeListOf<Element> = document.getElementsByTagName("game");
You could use type assertions to force it into an HTMLElement[]
, but this would give misleading type information and make your tools lie.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With