DOM types

TypeScript provides types for the DOM (HTMLElement, HTMLInputElement, Event, etc.). Selectors such as querySelector return an element or null, which you must check, and a more specific element requires narrowing or a type assertion. Event types also type event.target.

Example

const input = document.querySelector('#nom');
if (input instanceof HTMLInputElement) {
  console.log(input.value);
}

Related concepts

The whole glossary