Declaration Files
A declaration file (.d.ts) is TypeScript that describes types and stops there. It has no runtime code. The compiler uses it to type-check imports of JavaScript that has already been compiled or that was written by hand.
Detailed Explanation
TypeScript erases types when it emits JavaScript. The types still need to exist for anyone who imports that JavaScript from another TypeScript file. The compiler writes those leftover types into .d.ts files when declaration is on. A file src/user.ts becomes dist/user.js plus dist/user.d.ts. The .d.ts contains the exported types, the exported function signatures, and declared values. It does not contain function bodies.
You also write declaration files by hand in three situations.
First, a JavaScript library with no types. You add a vendor.d.ts (or a package in DefinitelyTyped, @types/name) that describes the API you call. The compiler matches imports to those declarations through module resolution.
Second, globals. declare const __DEV__: boolean or interface Window { analytics: Analytics } in a file that is part of the project (or referenced as a type root) makes those names exist in every file. This is how lib.dom works: it is a large set of .d.ts files shipped with the compiler.
Third, "ambient modules." declare module "some-untyped-pkg" { export function start(): void } gives a module a type without forking the package. declare module "*.svg" is the same pattern for bundler assets.
A .d.ts file is still TypeScript. It can use imports, generics, mapped types, and declaration merging. What it cannot do is emit. const x = 1 in a .d.ts is an error; declare const x: 1 is the form. Classes in declarations are shapes. Enums in declarations can be declare enum (a real object is expected at runtime) or const enum (inlined).
Publishing a library means shipping .d.ts next to .js, and pointing package.json "types" (or "exports" .d.ts conditions) at the entry file. tsc with "declaration": true is the usual way to generate them. Hand-writing the public .d.ts and implementing in JS is valid and harder to keep in sync.
.d.ts files are trusted. The compiler does not check them against the JavaScript they claim to describe, except in limited allowJs situations. A wrong declaration is a type hole that looks like a type. Treat generated declarations as the source of truth for your own packages, and treat @types/* as a third-party claim that can be wrong.
Code Examples
1. What tsc emits for a small module (the .d.ts).
// 1. What tsc emits for a small module (the .d.ts).
// src/ids.ts
export function parseId(raw: string): number {
return Number.parseInt(raw, 10);
}
// dist/ids.d.ts (generated)
export declare function parseId(raw: string): number;2. A hand-written ambient module for an untyped package.
// 2. A hand-written ambient module for an untyped package.
declare module "legacy-logger" {
export function info(message: string): void;
export function error(message: string, err?: unknown): void;
}
// usage in a .ts file
import { info } from "legacy-logger";
info("ready");3. A global and a bundler asset type.
// 3. A global and a bundler asset type.
declare const __APP_VERSION__: string;
declare module "*.css" {
const classes: Record<string, string>;
export default classes;
}
console.log(__APP_VERSION__);Common Gotchas
Implementation syntax is illegal in `.d.ts`
use `declare function`, `declare const`, and `export type`. A function body belongs in a `.ts` or `.js` file.
A declaration is a claim, not a proof
if the `.d.ts` says `foo(): string` and the JS returns `undefined`, the compiler will not notice. Reproduce the mismatch in a failing runtime test.
`skipLibCheck` skips errors inside other people's `.d.ts` files
useful for speed, bad when you are debugging a broken `@types` package.
Two `.d.ts` files that merge the same interface combine silently
that is how `Window` grows, and how a typo in a global interface pollutes the project.