Reputation: 2449
I have an error:
Property 'innerText' does not exist on type 'EventTarget'.
I'm trying to add event listener and get value from element. Everything works fine but this error shows up in console .
public componentDidMount() {
const element = document.querySelector(".mdc-list")
element.addEventListener("click", (e) => {
this.data.menu.title = e.target.innerText
})
}
Upvotes: 21
Views: 20512
Reputation: 3030
It is a TypeScript issue, cast the event.target to it’ type to tell TypeScript that it has the property you set for it.
const input = event.target as HTMLElement;
this.data.menu.title=input.innerText
Upvotes: 45
Reputation: 10157
You can either make a type guard to narrow down the event target type.
Or just cast the event target to whichever element you're getting as a target:
this.data.menu.title = <HTMLInputElement>(e.target).innerText;
Upvotes: 1