"play" does not exist in type "HTMLElement" and property "value" does not exist in type "EventTarget",

Not sure why these two warnings appeared. Any idea to get rid of this typescript warning? Thanks to

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)}/>

      

+3


source to share


3 answers


The first warning means that HTMLElement

there is no method on the instance play()

. This makes sense as it play()

is presumably a method that you have defined on your own component.

The second warning means there is EventTarget

no property in the instance value

. For more information on EventTarget

s see MDN
.

I'm not sure how to solve the first problem since you need to access the instance being created. But for the second, you most likely just have to provide a return value. So, you should do something like this:

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

      




EDIT . This might fix your first problem.

I'm not sure, but maybe you are trying to play a media element, and if so, you need to cast the audio player in the correct type:

let audioPlayer: HTMLMediaElement = document.getElementById(audioId)

      

See MDN for details.

+2


source


Late post, but it will help other users. To solve the first problem, you can change the type of the variable audioPlayer

to HtmlVideoElement

as shown below

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

      



This will drastically solve the problem.

+4


source


In Angular 5, you need to do the following:

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

audioPlayer.play();  

      

+1


source







All Articles