Configuring target and lib
Configuring target and lib — why it matters
target selects the JavaScript language level TypeScript emits; lib selects standard API declarations available to the checker. They are related but not identical: including dom allows browser types, but does not create DOM APIs in Node.js. Select a runtime-compatible target and libraries without assuming a polyfill is installed.
What you’ll learn
- Explain configuring target and lib and the problem it solves.
- Identify the relevant compile-time checks and runtime responsibilities.
- Run the example, predict its output, and try a realistic modification.
Understand the mechanism
The example uses modern Array APIs declared by an appropriate ES library. A modern runtime implements them, but an older runtime could require a polyfill even when TypeScript checks the calls successfully.
Adding "DOM" to lib will not make document exist in a Node process; libraries change declarations, not the environment.
Follow the concept flow
Run the primary TypeScript example
Save as configuring-target-lib.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.
TypeScript source
const names: string[] = ["Ada", "Lin", "Maya"];
console.log(names.at(-1));
console.log(names.includes("Lin"));Verified output
Maya
trueCompile: npx tsc --strict --target es2022 --module commonjs configuring-target-lib.ts; run: node configuring-target-lib.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.
The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.
Trace the result
The example uses modern Array APIs declared by an appropriate ES library. A modern runtime implements them, but an older runtime could require a polyfill even when TypeScript checks the calls successfully.
Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.
Try the full tool or configuration workflow
Practical project lab: These are real configuration and source-file examples, not a TypeScript object that merely describes settings. Place the files at the named paths relative to one project directory.
tsconfig.json
{"compilerOptions":{"target":"ES2022","lib":["ES2022","DOM"],"module":"ES2022","strict":true}}main.ts
const element = document.querySelector("h1");
console.log(element?.textContent);Commands
npx tsc --noEmit -p .DOM declarations enable compiling the browser code; run the emitted code in a browser, not Node.
This local compiler or test-runner mini-project was validated in addition to the primary program.
Common mistake and correction
Adding "DOM" to lib will not make document exist in a Node process; libraries change declarations, not the environment.
Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.
Practice the concept
Type-check the sample with lib es2022,dom; compare diagnostics after selecting an older ES-only lib.
Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.
Check your understanding
Official reference
For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.
What to remember
target selects the JavaScript language level TypeScript emits; lib selects standard API declarations available to the checker.
Run the primary example, then complete the practice task or environment-specific lab.
Reaching the recap and completing the quizzes are separate milestones.