Property 'value' does not exist on type 'EventTarget & Element'. code example

Example 1: property 'name' does not exist on type 'eventtarget' react

// If you have to use event.target itself, you would have to cast the object:

const { name } = event.target as HTMLButtonElement;

Example 2: Property 'value' does not exist on type 'EventTarget & Element'.

(e.target as HTMLButtonElement).value

Example 3: Property 'on' does not exist on type 'HTMLElement'.

To prevent this error you can write:

var plotDiv: any = document.getElementById('myDiv');
plotDiv.on('plotly_relayout', ...
document.getElementById('myDiv') return HTMLElement. This type doesn't contain method
on because this method is added within plotly-latest.min.js. So in order to silence
the typescript warning you can explicity say compile not to check types for plotDiv

Another way is create type definition like:

interface PlotHTMLElement extends HTMLElement  {
  on(eventName: string, handler: Function): void;
}

var plotDiv  = <PlotHTMLElement>document.getElementById('myDiv')
plotDiv.on('plotly_relayout', function() {

});

Example 4: REACT TS roperty 'value' does not exist on type 'EventTarget & Element'

@Zyon Lyod has already provided the code and some explanation but i will add
more details:
The problem is that not all html events have the value property. And thus not all
event.target have that property since event.target is a generic html element
so you need to cast to the html element that you are using
You are welcome,

Tags: