DOM Types
DOM types are the TypeScript interfaces for the browser platform: Document, HTMLElement, HTMLInputElement, Event, NodeListOf<T>. They come from lib.dom and describe the real objects the engine gives you.
Detailed Explanation
When lib includes DOM, the compiler knows document, window, and the element class hierarchy. document.getElementById returns HTMLElement | null. document.querySelector("input") returns Element | null in the default typing, not HTMLInputElement, because the selector is just a string. querySelector<HTMLInputElement>("input") is a type argument that asserts the result; a wrong selector still returns a div at runtime.
Narrowing is how you get from Element to a specific tag. instanceof HTMLInputElement is the honest check. A tag-name map (HTMLElementTagNameMap) is what createElement("input") uses to return HTMLInputElement. document.createElement("input") is therefore precise. createElement(tag: string) is HTMLElement.
NodeListOf<T> and HTMLCollectionOf<T> are the typed collections. They are array-like, not arrays. [...nodeList] is T[]. forEach exists on NodeList. Index reads have the same T vs T | undefined issue as arrays.
Nullability is everywhere. parentElement is HTMLElement | null. firstChild is ChildNode | null. strictNullChecks is what makes those | nulls real. The DOM lib was written for that flag.
Events are a separate map (HTMLElementEventMap) so addEventListener("click", e => ...) types e as MouseEvent. See event types.
Do not copy DOM interfaces into your app types. If you need a serializable user, write User. If you need a handle to a node, keep HTMLElement inside the UI layer and do not put it in a Redux store type unless you mean to retain the node (and leak).
Server-side TypeScript should not include DOM in lib unless you truly have a DOM (jsdom). Accidental document use on the server is a type error when lib is ES2022 only. That is a feature.
See lib.dom for how the files are shipped, and the JavaScript DOM page for the platform behavior.
Code Examples
1. getElementById is HTMLElement | null; instanceof narrows.
// 1. getElementById is HTMLElement | null; instanceof narrows.
function valueOf(id: string): string | undefined {
const el = document.getElementById(id);
if (!(el instanceof HTMLInputElement)) return undefined;
return el.value;
}2. createElement uses the tag name map.
// 2. createElement uses the tag name map.
const input = document.createElement("input");
input.type = "email";
const generic = document.createElement("div");
// generic.value
// Error: Property 'value' does not exist on HTMLDivElement.3. querySelector is Element | null unless you pass a type argument (an assertion).
// 3. querySelector is Element | null unless you pass a type argument (an assertion).
const maybe = document.querySelector("input");
if (maybe instanceof HTMLInputElement) {
console.log(maybe.value);
}Common Gotchas
`querySelector<HTMLInputElement>` does not check the selector
a `"div"` selector still type-checks. Prefer `instanceof`.
`NodeList` is not `T[]`
some array methods are missing. Spread or `Array.from` if you need them.
`null` is the DOM's empty value, not `undefined`
convert at your app boundary if the rest of the code uses `undefined`.
Including `DOM` in a Node `lib` hides server-side `document` bugs
split tsconfigs.