类型'ChildNode [Typescript]上不存在属性'tagName'

卡莱布·泰勒(Caleb Taylor)

我从父节点遍历childNodes。我想访问子节点的tagName,但是错误是在ChildNode类型上不存在tagName。

const contentParsed = new DOMParser().parseFromString(content, 'text/xml');
const svgBody = contentParsed.querySelector('svg');

if (svgBody === null) throw new Error('content must be svg');
svgBody.childNodes.forEach((node) => node.tagName) // Property 'tagName' does not exist on type 'ChildNode'.ts(2339)

它适用于javascript

const svgStr = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><title></title><path d="M 2.6846088,10.906645 H 28.979394 v 8.389403 H 15.580319 l -0.719092,1.438183 -3.811186,0.143819 -0.575273,-1.582002 H 2.6846088 Z" fill="#fff"/><path d="M2,10.555H30v9.335H16v1.556H9.778V19.889H2Zm1.556,7.779H6.667V13.666H8.222v4.667H9.778V12.111H3.556Zm7.778-6.223v7.779h3.111V18.334h3.111V12.111Zm3.111,1.556H16v3.112H14.444Zm4.667-1.556v6.223h3.111V13.666h1.556v4.667h1.556V13.666h1.556v4.667h1.556V12.111Z" fill="#cb3837"/></svg>`
const contentParsed = new DOMParser().parseFromString(svgStr, 'text/xml');
const svgBody = contentParsed.querySelector('svg');

if (svgBody === null) throw new Error('content must be svg');
svgBody.childNodes.forEach(node => console.log(node.tagName))

莫蒂

childNodes可以包含非元素节点,请children改用。

它可以在JavaScript中使用,因为JS的类型安全性不高,并且您的父节点碰巧只有元素作为子元素(或打印为undefined)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeScript - 类型上不存在属性(但它存在)?

TypeScript:禁止联合类型的“类型上不存在属性”

TypeScript + Express:类型“ IncomingMessage”上不存在属性“ rawBody”

与 TypeScript 反应 - 类型“EventTarget”上不存在属性“value”

类型'FileList'上不存在TypeScript属性'forEach'

TypeScript Joi:类型“ Root”上不存在属性“ validate”

Typescript 属性在类型元素上不存在

TypeScript错误:类型“ Navigator”上不存在属性“ app”

Typescript中的类型“ Date”上不存在属性“ getWeek”

TypeScript:类型上不存在属性“范围”

ReactJS 中的 Typescript 错误 - 类型上不存在属性

Typescript属性“ length”在类型上不存在

属性“名称”在类型“ EventTarget”上不存在-React + TypeScript

Typescript 的“对象”错误类型上不存在属性

TypeScript:类型“any[]”上不存在属性“name”

类型'ElementFinder'上不存在Typescript属性

TypeScript:类型“ Function”上不存在属性“ propertyName”

VueJS + Typescript:类型上不存在属性

Typescript抱怨PostResponse []类型上不存在属性名称

扩展类型上不存在 Typescript 属性

“类型{}上不存在属性json” TypeScript

TypeScript编译错误-类型上不存在属性

联合体类型上不存在Typescript属性

类型“NavigateFunction”Typescript 上不存在属性“push”

TypeScript类型不存在属性“ load”?

TypeScript中的类型不存在属性

Typescript:组合类型不存在属性?

枚举类型不存在Typescript属性

类型“ ChildNode”缺少类型“ HTMLOptionElement”中的以下属性