wiki/content/20201001105545-lib_d_ts.md

1.4 KiB

id title
afc1ca85-af24-4ef6-bcd8-91193c4fbd32 lib.d.ts

Introduction

This is where TypeScript "cheats" and ships with a file called lib.d.ts. This file contains ambient declarations for a whole bunch of common JavaScript constructs present in JavaScript runtimes and the DOM.

  • This file is automatically included in the compilation context of a TypeScript project.
  • The objective of this file is to make it easy for you to start writing type checked JavaScript code.

Modifying Native Types

If you want to be a clever clogs you can add stuff to the interfaces in lib.d.ts by creating a global module (ie: global.d.ts)

Example

lib.d.ts magic

The following code is only possible because of lib.d.ts. If it's used with the nolib option, TypeScript doesn't know that all JavaScript objects have a toString function.

var foo = 123;
var bar = foo.toString();

Example of what's in the lib.d.ts

declare var window: Window;

interface Window extends EventTarget, WindowTimers, WindowSessionStorage, WindowLocalStorage, WindowConsole, GlobalEventHandlers, IDBEnvironment, WindowBase64 {
    animationStartTime: number;
    applicationCache: ApplicationCache;
    clientInformation: Navigator;
    closed: boolean;
    crypto: Crypto;
    // so on and so forth...
}