Home > Software engineering >  Typescript: why array from HTMLCollection has Element[] type
Typescript: why array from HTMLCollection has Element[] type

Time:08-12

Let's say I have an HTMLCollection:

const appElem = document.querySelector("#app");
const appChildren = appElem?.children; // HTML collection

now if I convert this collection to an array it will be Element[]:

const appChildrenArr = appChildren && Array.from(appChildren); // Element[]

But why appChildrenArr has Element[] type, not HTMLElement[] type?

Sandbox

CodePudding user response:

Because they don't have to be HTMLElements. You could have an SVGElement there, as well, for example.

https://developer.mozilla.org/en-US/docs/Web/API/Element

  • Related