252/361

What are `.d.ts` declaration files and when do you write them?

Quiz · TypeScript
Medium
7 min
typescript
declaration-files
tooling

TL;DR

A declaration file (.d.ts) contains only type information: no implementations, and it emits no JavaScript. It describes the shape of JavaScript code so TypeScript can type-check against it. They come from three places: generated by tsc with declaration: true (how TS libraries publish types), bundled by JS libraries (the types field or exports conditions in package.json), or community-maintained in DefinitelyTyped (@types/*). You write them by hand to type untyped JS modules, globals, non-code imports (*.svg, *.css), and augmentations.

What goes in one

// math-utils.d.ts (types for a plain JS module math-utils.js)
export interface RoundOptions {
  decimals?: number;
  mode?: 'floor' | 'ceil' | 'round';
}
export declare function round(value: number, options?: RoundOptions): number;
export declare const VERSION: string;
export default round;

Everything is a declaration (declare function, declare const, interface, type). Bodies aren't allowed.

Common hand-written cases

Untyped package (no @types available):

// types/legacy-lib.d.ts
declare module 'legacy-lib' {
  export function init(apiKey: string): void;
  export function track(event: string, props?: Record<string, unknown>): void;
}

// Quick, unsafe stopgap: everything is any
declare module 'some-untyped-lib';

Assets imported through a bundler:

declare module '*.svg' {
  const url: string;
  export default url;
}
declare module '*.module.css' {
  const classes: Readonly<Record<string, string>>;
  export default classes;
}

(Vite ships these in vite/client types. Reference them with /// <reference types="vite/client" /> or via tsconfig types.)

Globals from script tags:

// globals.d.ts (script file, no imports or exports, so declarations are global)
declare const __APP_VERSION__: string;
interface Window { Intercom?: (cmd: string, ...args: unknown[]) => void }

How TypeScript finds types for a package

  1. The exports map's "types" condition (put it first in each condition block), or the top-level types/typings field.
  2. A .d.ts file next to the resolved .js file (index.js → index.d.ts).
  3. node_modules/@types/<pkg> (auto-included unless the tsconfig types option restricts it).

With moduleResolution: "nodenext" or "bundler", the exports map is respected, and dual ESM/CJS packages may need separate .d.mts/.d.cts files. Mismatches cause "masquerading as CJS/ESM" problems, which the Are The Types Wrong tool detects.

Publishing a TypeScript library

{
  "compilerOptions": { "declaration": true, "declarationMap": true, "emitDeclarationOnly": false }
}

declarationMap lets "go to definition" jump to your .ts source. TS 5.5's isolatedDeclarations requires explicit return types on exports, so other tools can generate .d.ts files in parallel without type-checking.

skipLibCheck

skipLibCheck: true skips type-checking all .d.ts files (yours and dependencies'). It's faster and avoids conflicts between @types versions, but it can hide errors in your own hand-written declarations. Many projects enable it anyway.

Pitfalls

  • Script vs module semantics: adding an import to a globals file turns it into a module, so its declarations stop being global (use declare global).
  • Drift: hand-written declarations aren't checked against the JS implementation. Keep them minimal and test them with type tests.
  • @types version mismatches with the runtime package version.
  • Overusing declare module 'x'; (everything any) as a permanent fix.

In an interview, mention…

  • Types-only files that describe JS, and where they come from (tsc, bundled types, DefinitelyTyped).
  • Writing declarations for untyped modules, assets and globals.
  • Resolution order with exports/types, and dual-package pitfalls.
  • declaration/declarationMap, isolatedDeclarations and skipLibCheck trade-offs.

Spotted a mistake or a rights concern? Report an issue