Course contentsShow
← TypeScript
Lesson 54 of 1,7962. Setup and ToolingFree lesson

lib: Specifying Available Type Definitions

Declaring which built-in library type definitions should be included (DOM, ES2015, etc.) based on your target environment.

lib: Specifying Available Type Definitions

What You'll Learn

You'll learn how to tell TypeScript which built-in JavaScript features exist in your runtime environment so it can provide accurate type checking and autocomplete.

The Core Problem

JavaScript has evolved over many years, and different environments (browsers, Node.js) support different features. Your browser has document and window, but Node.js doesn't. ES2015 introduced Promise, but older environments don't have it. TypeScript needs to know what exists in your target environment to catch errors correctly.

Think of lib like telling TypeScript which dictionary to use. If you're writing Spanish, you need a Spanish dictionary. If you're mixing Spanish and French, you need both dictionaries.

How lib Works

The lib option in tsconfig.json specifies which type definition libraries TypeScript should include. These aren't runtime code—they're just descriptions of what exists:

{
  "compilerOptions": {
    "target": "ES5",
    "lib": ["ES2015", "DOM"]
  }
}

This configuration says: "I'm compiling to ES5, but my environment supports ES2015 features and browser APIs." TypeScript will now recognize Promise, Map, Set (ES2015) plus document, window, fetch (DOM).

Common lib Values

  • "ES5", "ES2015", "ES2016", etc. — JavaScript language features
  • "DOM" — Browser APIs (document, window, etc.)
  • "WebWorker" — Web Worker APIs
  • "ESNext" — Cutting-edge JavaScript features

If you omit lib, TypeScript chooses defaults based on your target. But specifying it explicitly gives you control—like targeting ES5 output while still using modern APIs that you've polyfilled.

Key Takeaway: The lib option tells TypeScript which built-in JavaScript and environment APIs exist in your runtime, enabling accurate type checking without affecting your compiled output.