简体   繁体   English

类型“元素”上不存在属性“innerText”

[英]Property 'innerText' does not exist on type 'Element'

I'm using Typescript for Puppeteer.我正在为 Puppeteer 使用 Typescript。 I'm trying to get innerText from an element.我正在尝试从元素中获取 innerText。

const data = await page.$eval(selector, node => node.innerText);

I'm getting the error:我收到错误:

Property 'innerText' does not exist on type 'Element'类型“元素”上不存在属性“innerText”

I tried casting to HTMLElement like described in this question: error " Property 'innerText' does not exist on type 'EventTarget' "?我尝试像这个问题中描述的那样转换为 HTMLElement: 错误“属性‘innerText’在类型‘EventTarget’上不存在”?

const data = await page.$eval(selector, <HTMLElement>(node: HTMLElement) => node.innerText);

As well as creating my own Interface like this:以及像这样创建我自己的界面:

interface TextElement extends Element {
    innerText: string;
}

But in each case I'm getting the error that innerText does not exist on this particular type.但在每种情况下,我都会收到此特定类型上不存在 innerText 的错误。

Property 'innerText' does not exist on type 'HTMLElement'类型“HTMLElement”上不存在属性“innerText”

Why is this happening and how to deal with this?为什么会发生这种情况以及如何处理?

You can fix it like this:你可以像这样修复它:

const data = await page.$eval(selector, node => (node as HTMLElement).innerText);

or:或者:

const data = await page.$eval(selector, node => (<HTMLElement>node).innerText);

UPDATE:更新:

So, after some exchange on Github , it's clear why the syntax from this question does not work.因此,在Github上进行了一些交流之后,很明显为什么这个问题的语法不起作用。 It actually defines anonymous generic function.它实际上定义了匿名泛型函数。

<HTMLElement>(node: HTMLElement) => node.innerText

Clearer example would be following:更清楚的例子如下:

function myFunction<T>(node: T) {
    node.innerText; // Property 'innerText' does not exist on 'T'
}

const data = await page.$eval(selector, myFunction);

Unfortunate naming of T as HTMLElement creates confusion.不幸的是,将T命名为HTMLElement会造成混淆。

For those who needs to fix this error for any element returned from querySelector :对于那些需要为从querySelector返回的任何元素修复此错误的人:

const el = document.querySelector<HTMLElement>('#app');

if (el) {
  el.innerText = content;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'Element'.ts 类型上不存在属性'innerText'(2339) - Property 'innerText' does not exist on type 'Element'.ts (2339) “EventTarget”类型上不存在属性“innerText”--TypeScript - Property 'innerText' does not exist on type 'EventTarget' --TypeScript 错误“属性&#39;innerText&#39;在&#39;EventTarget&#39;类型上不存在”? - error “ Property 'innerText' does not exist on type 'EventTarget' ”? 类型“元素”上不存在属性“样式” - Property 'style' does not exist on type 'Element' “元素”类型上不存在属性“yPos” - Property 'yPos' does not exist on type 'Element' 类型元素上不存在属性attachshadow - property attachshadow does not exist on type element “元素”类型上不存在属性“值” - Property 'value' does not exist on type 'Element' 类型“NodeListOf”上不存在属性“removeChild”<element> '</element> - Property 'removeChild' does not exist on type 'NodeListOf<Element>' Typescript 错误“类型‘HTMLElement’上不存在属性‘值’”、“‘元素’类型上不存在属性‘onclick’” - Typescript error "Property 'value' does not exist on type 'HTMLElement'", "Property 'onclick' does not exist on type 'Element'" 为什么html元素的innerText属性只显示body元素的innerText? - Why does the innerText property of the html element only show the innerText of the body element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM