Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the array type for html elements in typescript?

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");
like image 852
Kokodoko Avatar asked Apr 08 '16 10:04

Kokodoko


1 Answers

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.

like image 66
Fenton Avatar answered Nov 13 '22 10:11

Fenton