Skip to main content

Lesson 6 of 8

Declaration Files & @types

Declaration files (`.d.ts`) describe the shape of JavaScript code to TypeScript - without any implementation. They are how TypeScript types third-party JS libraries. Understanding them lets you type untyped dependencies, extend global types, and publish typed libraries.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Declaration Files - Writing & Consuming Types

Declaration Files - Writing & Consuming Types
// ── .d.ts files - type declarations only, no JS output ──────
// lib/utils.d.ts (describes lib/utils.js which has no types)
export declare function formatDate(date: Date, format: string): string;
export declare function slugify(text: string): string;
export declare const DEFAULT_LOCALE: string;

// ── @types packages - community type definitions ───────────────
// npm install --save-dev @types/node     # Node.js: fs, path, process, http
// npm install --save-dev @types/express  # Express.js
// npm install --save-dev @types/jest     # Jest testing framework

// TypeScript looks for @types/* in node_modules automatically.
// If a package ships its own types (e.g., zod), no @types needed.

// ── Declaration merging - extend global types ─────────────────
// Extend Express Request with your auth user:
// src/types/express.d.ts
declare global {
  namespace Express {
    interface Request {
      user?: { id: number; email: string; role: "member" | "admin" };
    }
  }
}
export {};  // must be a module (has export/import) for global augmentation

// Now in any route handler:
app.get("/profile", (req, res) => {
  if (!req.user) return res.status(401).json({ error: "Unauthorized" });
  res.json({ user: req.user });   // ✅ req.user is typed
});

// ── Augment environment variables - global declaration ─────────
// src/types/environment.d.ts
declare global {
  namespace NodeJS {
    interface ProcessEnv {
      NODE_ENV: "development" | "test" | "production";
      DATABASE_URL: string;
      JWT_SECRET: string;
      PORT?: string;
      // Add every expected env var - TS will warn if you use an undeclared one
    }
  }
}
export {};

// ── Typing untyped modules - ambient module declarations ───────
// src/types/untyped.module.d.ts
declare module "legacy-lib" {
  export function legacyFn(input: string): number;
  export const VERSION: string;
}

// ── skipLibCheck and why NOT to disable it ────────────────────
// "skipLibCheck": true  → skips .d.ts files in node_modules
// Performance: much faster builds.
// Safety: you lose type errors from @types packages.
// Recommendation: keep skipLibCheck: true, rely on your OWN types.

Common Mistakes

  • Not using `export {}` in .d.ts augmentation files - without `export {}` or any import/export, TypeScript treats the file as a script (ambient context). Declaration merging into global namespaces only works in module files.
  • Writing `.d.ts` files for TypeScript source - TypeScript generates `.d.ts` automatically from `.ts` files with `declaration: true`. Only write manual `.d.ts` for vanilla JS libraries that don't have types.
  • Forgetting to include `.d.ts` in paths tsconfig include/files - custom `.d.ts` declaration files must be included in your `tsconfig.json` (either via `include` glob or explicitly listed in `files`).

Tip

Tip

Practice Declaration Files types in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Declaration Files & @types

Practice Task

Note

Practice Task - (1) Write a working example of Declaration Files types from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Declaration Files types is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • Declaration files (`.
  • Not using `export {}` in .d.ts augmentation files - without `export {}` or any import/export, TypeScript treats the file as a script (ambient context). Declaration merging into global namespaces only works in module files.
  • Writing `.d.ts` files for TypeScript source - TypeScript generates `.d.ts` automatically from `.ts` files with `declaration: true`. Only write manual `.d.ts` for vanilla JS libraries that don't have types.
  • Forgetting to include `.d.ts` in paths tsconfig include/files - custom `.d.ts` declaration files must be included in your `tsconfig.json` (either via `include` glob or explicitly listed in `files`).

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done