TypeScript theorytheory 0/50 · 0%
Tooling · medium

32. Module augmentation & declaration files

Describing the shape of untyped JS and extending existing types.

A `.d.ts` file contains only type declarations, no runtime code. It's used to describe the shape of plain JavaScript libraries, or to add global types to a project.

ts
// globals.d.ts
declare global {
  interface Window {
    ethereum?: { request: (args: { method: string }) => Promise<unknown> };
  }
}
export {};

Module augmentation lets you add to an existing module's types without modifying its source, e.g. extending a third-party library's interface with an extra field your app relies on. Many npm packages ship their own `.d.ts` (or point to `@types/<package>` on DefinitelyTyped) so consumers get type checking without the library itself being written in TypeScript.

Check your understanding

  1. 1. What does a `.d.ts` file contain?

  2. 2. What is 'module augmentation' used for?

  3. 3. Where do many untyped JS packages get their type definitions from?