Not sure why these two warning came from. Any idea to get rid of those warning message from typescript? Thanks
export function playAudio(e, audioId) {
e.preventDefault()
let audioPlayer: HTMLElement = document.getElementById(audioId)
audioPlayer.play();
}
<input type="checkbox" id="inputSchedule" name="inputCheckboxesSchedule"
value={this.state.displayMode}
onClick={e => this.toogleDisplaymode(e.target.value)}/>
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.
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.
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.
In Angular 5 you need to cast it as follows:
let audioPlayer = <HTMLVideoElement> document.getElementById(audioId);
audioPlayer.play();
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 EventTarget
s, 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.
In angular 7 works this code:
let audioPlayer = <HTMLVideoElement>document.getElementById('video');
audioPlayer.muted = true;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With