Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'play' does not exist on type 'HTMLElement' and Property 'value' does not exist on type 'EventTarget'

Tags:

typescript

jsx

Not sure why these two warning came from. Any idea to get rid of those warning message from typescript? Thanks

error TS2339: Property 'play' does not exist on type 'HTMLElement'.

    export function playAudio(e, audioId) {
        e.preventDefault()
        let audioPlayer: HTMLElement = document.getElementById(audioId)
        audioPlayer.play();
    }

(248,82): error TS2339: Property 'value' does not exist on type 'EventTarget'.

    <input type="checkbox" id="inputSchedule" name="inputCheckboxesSchedule"
           value={this.state.displayMode}
           onClick={e => this.toogleDisplaymode(e.target.value)}/>
like image 274
newBike Avatar asked May 21 '17 17:05

newBike


People also ask

Does not exist on type EventTarget?

The error "Property 'value' does not exist on type 'EventTarget'" occurs when we try to access the value property on an element that has a type of EventTarget . To solve the error, use a type assertion to type the element correctly before accessing the property. This is the index.

How do you type an event target in TypeScript?

Use a type assertion to type event. target in TypeScript, e.g. const target = event. target as HTMLInputElement . Once typed correctly, you can access any element-specific properties on the target variable.

What is event target value in Javascript?

The target event property returns the element that triggered the event. The target property gets the element on which the event originally occurred, opposed to the currentTarget property, which always refers to the element whose event listener triggered the event.


4 Answers

In Angular 5 you need to cast it as follows:

let audioPlayer = <HTMLVideoElement> document.getElementById(audioId);

audioPlayer.play();  
like image 169
Sem Avatar answered Oct 09 '22 17:10

Sem


The first warning means that there is no play() method on an HTMLElement instance. This makes sense since play() is presumably a method that you have defined on your own component.

The second warning means that there is no value property in the EventTarget instance. For more information about EventTargets, see MDN.

I am not sure how to solve the first problem since you need to grab access to an instance that you have created. But for the second one, you most likely just need to cast the return value. So, you should do something like this:

{e => this.toogleDisplaymode((e.target as HTMLInputElement).value)}

EDIT: This might solve your first problem.

I am not sure, but it might be that you are trying to play a media element and if that's the case, you need to cast the audioPlayer to the right type:

let audioPlayer: HTMLMediaElement = document.getElementById(audioId)

See MDN, again, for more information.

like image 35
Andrew Eisenberg Avatar answered Oct 09 '22 15:10

Andrew Eisenberg


In angular 7 works this code:

let audioPlayer = <HTMLVideoElement>document.getElementById('video');
audioPlayer.muted = true;
like image 26
qwerty Avatar answered Oct 09 '22 17:10

qwerty


A late post but it will help other users, To solve the first problem you can change type of audioPlayer variable to HtmlVideoElement as below

let audioPlayer: HTMLVideoElement = document.getElementById(audioId);  
audioPlayer.play();  

This will definately resolve the problem

like image 21
Amit Bisht Avatar answered Oct 09 '22 16:10

Amit Bisht