简体   繁体   English

Typescript:为什么来自 HTMLCollection 的数组有 Element[] 类型

[英]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?但是为什么appChildrenArrElement[]类型,而不是HTMLElement[]类型?

Sandbox 沙盒

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.

相关问题 为什么htmlCollection不从数组继承 - Why doesn't htmlCollection inherit from array 从HTMLCollection创建数组后,为什么image的clientHeight为0? - Why is clientHeight of images 0 after creating array from HTMLCollection? 在打字稿中对对象数组进行排序-元素隐含具有任何类型 - Sort array of objects in typescript - element implicty has any type HTMLCollection 从对象中获取每个元素 - HTMLCollection get every element from object HTMLCollection是一个数组吗? - Is HTMLCollection An Array? TypeScript或JS-如何选择元素及其所有子元素进入HTMLCollection? - TypeScript or JS- How to select an element and all its children into a HTMLCollection? Typescript,'NodeListOf<element> ' 不是数组类型或字符串类型</element> - Typescript,'NodeListOf<Element>' is not an array type or a string type TypeScript:元素隐含地具有 RegExp 的“任何”类型 - TypeScript: Element implicitly has an 'any' type for RegExp 为什么我在这里使用 TypeScript 得到“绑定元素‘商店’隐式具有‘任何’类型”? - Why do I get "Binding element 'store' implicitly has an 'any' type" here with TypeScript? 为什么打字稿抛出绑定元素“目标”隐式具有“任何”type.ts(7031)? - why typescript throws Binding element 'target' implicitly has an 'any' type.ts(7031)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM