Work with DOM Elements in TypeScript using Type Assertions

Share this video with your friends

Send Tweet

The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going to get, so you have to educate your codebase into what you know you're using. This is done using Type Assertion where TypeScript types know they're a certain type, but you give it additional information so you can access the properties and methods that you know will be available.

Navneet Kumar Sharma
Navneet Kumar Sharma
~ 5 years ago

How to autocomplete jsdoc options?

Lauro Silva
Lauro Silva
~ 4 years ago

Hey Navneet - The way you choose will depend on your project requirements, as well as in your preferences and needs. A good starting point is checking out this resource. Hope that helps!

~ 3 years ago

I'm getting an error with:

const input = document.getElementById("input") as HTMLInputElement; input.autofocus = true;

telling me that "Property 'autofocus' does not exist on type 'HTMLInputElement'.ts(2339)"