Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting HTMLElement to HTMLAudioElement

Tags:

typescript

I have this line of typescript:

var audio: HTMLAudioElement = document.getElementById("audioElement");

However, I get the build error:

Type HTMLElement is not assignable to type 'HTMLAudioElement'.

Is there a different way I should be retrieving the element as an HTMLAudioElement, or do I just cast it to HTMLAudioElement?

like image 359
Greg Gum Avatar asked Dec 10 '22 22:12

Greg Gum


1 Answers

The casting sutras for typescript looks like this:

const audio = document.getElementById("audioElement") as HTMLAudioElement;

And you can either specify the type on the audio variable if you want, but typescript will pick that up automatically for you.

like image 184
Brocco Avatar answered Jan 31 '23 05:01

Brocco