简体   繁体   中英

Typescript: why array from HTMLCollection has Element[] type

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM