[英]Typescript: why array from HTMLCollection has Element[] type
Let's say I have an HTMLCollection:假设我有一个 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[]
:现在,如果我将此集合转换为数组,它将是
Element[]
:
const appChildrenArr = appChildren && Array.from(appChildren); // Element[]
But why appChildrenArr
has Element[]
type, not HTMLElement[]
type?但是为什么
appChildrenArr
有Element[]
类型,而不是HTMLElement[]
类型?
Because they don't have to be HTMLElements.因为它们不必是 HTMLElements。 You could have an SVGElement there, as well, for example.
例如,您也可以在那里拥有一个 SVGElement。
https://developer.mozilla.org/en-US/docs/Web/API/Element https://developer.mozilla.org/en-US/docs/Web/API/Element
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.