lib.dom
lib.dom is the built-in declaration bundle for the browser: window, document, fetch, HTMLElement, and the rest of the web IDL as TypeScript sees it. You opt in with "lib": ["DOM", ...] or a target that includes it.
Detailed Explanation
TypeScript ships a set of .d.ts files named after lib options: lib.es2022.d.ts, lib.dom.d.ts, lib.dom.iterable.d.ts, lib.webworker.d.ts. They are not polyfills. They do not add fetch to old browsers. They tell the checker what names exist if you assume that environment.
lib.dom.d.ts is large. It declares global Window, merges into it, and lists element interfaces generated from the spec (with a lag). lib.dom.iterable adds iterable versions of NodeList and friends. lib.webworker is the worker global (self, no document). You cannot include both DOM and WebWorker in one project without conflicts; split the worker's tsconfig.
If you omit lib, TypeScript picks defaults from target. A target of ES2022 without an explicit lib may not include DOM. A tsc --init for a web app usually writes "lib": ["ES2022", "DOM"]. A Node app should list ES2022 (and maybe DOM only in a frontend package).
You can augment lib.dom. interface Window { analytics: Analytics } in a project .d.ts merges. That is how you add a script-tag global. You can also replace a piece with a newer @types package, but fighting lib.dom is rarely worth it; bump TypeScript instead.
skipLibCheck skips errors inside lib.dom and other .d.ts files. It will not skip errors in your code that use document. Turn skipLibCheck on for speed; do not expect it to hide a missing DOM lib.
Versioning: the DOM lib tracks the TypeScript release, not your users' browsers. Array.at in ES2022 and crypto.randomUUID in DOM can type-check and then crash on an old engine. "lib" is a claim about the types you want, not the engines you support. Pair it with a target and a runtime support matrix.
Triple-slash /// <reference lib="dom" /> pulls the same files into one file. Prefer tsconfig lib for the project.
Code Examples
1. A browser package.
// 1. A browser package.
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"strict": true
}
}2. A Node package — no DOM.
// 2. A Node package — no DOM.
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022"],
"types": ["node"],
"strict": true
}
}3. Augmenting Window.
// 3. Augmenting Window.
interface Window {
__APP_VERSION__: string;
}
console.log(window.__APP_VERSION__);Common Gotchas
`document` is undefined in types without `DOM`
if the editor knows `document` and CI does not, the `lib` arrays differ.
`lib` is not a polyfill
typing `fetch` does not add `fetch` to IE. Check the engine.
`DOM` and `WebWorker` globals collide
give the worker its own tsconfig with `lib`: `["ES2022", "WebWorker"]`.
Augmenting `Window` merges globally
a typo in a `.d.ts` becomes a real global type. Keep augmentations in one file.