Exclude browser declarations
A Node or server project should not expose browser globals such as document and window to application code.
TypeScript · Const
The lib compiler option selects the JavaScript and environment API declarations available during type checking. Use it to align TypeScript's known APIs with the actual runtime, browser platform, and polyfills in a project.
The lib compiler option controls which declaration files describe available JavaScript and environment APIs. It can replace the defaults inferred from target, letting a project include only the platform and ECMAScript features its runtime supplies, such as ES libraries, DOM types, or selected higher-level APIs.
{
"compilerOptions": {
"lib": ["ES2020", "DOM"]
}
}| Name | Type | Description |
|---|---|---|
compilerOptions.lib | string[] | An array of library names, such as "ES2020" or "DOM", that selects the declaration files included in the program. |
No runtime return value; this is a tsconfig compiler option.
{
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020", "DOM"]
}
}
// src/main.ts
const root = document.querySelector("main");
const values = new Map<string, number>();
values.set("answer", 42);
console.log(root, values.get("answer"));This configuration supplies type definitions for ES2020 APIs and browser DOM globals, allowing the file to use Map and document with type checking.
A Node or server project should not expose browser globals such as document and window to application code.
A browser application needs DOM types alongside the ECMAScript APIs provided by its chosen runtime.
A project uses polyfills or native runtime features that are newer than the syntax target.
A large codebase needs its declared platform surface to match a specific execution environment.
The lib option selects declaration files for built-in JavaScript APIs and platform APIs. For example, ES2015 supplies APIs such as Promise, Map, Set, and Symbol, while DOM supplies browser globals such as window and document.
Follow-up: How would you type-check browser APIs while emitting for an older JavaScript runtime? Set lib to include DOM and the required ECMAScript API declarations, then choose an appropriate lower target for emitted syntax.
TypeScript normally chooses a default library set based partly on target. Explicit lib entries let a project remove unsuitable environment declarations, such as DOM types in a non-browser program, or add declarations for APIs supplied by polyfills.
Follow-up: Why might lib include a newer ES library than target? Because a runtime or polyfill may provide those APIs even though the compiler still downlevels newer syntax.
lib entries name library groups rather than individual declaration filenames. The page lists high-level libraries such as ES5, ES2015, ES2016, and DOM, with aliases such as ES6 for ES2015 and ES7 for ES2016.
Follow-up: What should you do if only part of a newer standard is available? Select the relevant granular library entries rather than claiming the entire higher-level standard library.
targetnoLibtypesexplainFiles/// <reference lib="..." />