Sub topic 9
Declaration files: describing code without implementing it
Assume you already know TypeScript interfaces and functions, can run tsc, and understand basic import and export statements. This chapter focuses on how .d.ts files describe JavaScript or TypeScript code for the type checker.
What a declaration file does
A declaration file has the .d.ts extension. It contains type information but no runtime implementation. TypeScript uses it to answer questions such as:
- Which functions does this module export?
- What arguments does a function accept?
- What type does it return?
- Which properties does an object have?
The declarations help your editor and compiler. Node.js does not execute them.
For example, this implementation file exports a function:
// src/index.ts
export function add(a: number, b: number): number {
return a + b;
}A matching declaration file describes the public function without its body:
// dist/index.d.ts
export declare function add(a: number, b: number): number;The declare keyword means that the implementation exists somewhere else. The declaration file does not create an add function at runtime.
Generating declarations from TypeScript
Set declaration to true in tsconfig.json:
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"declaration": true,
"outDir": "dist"
},
"include": ["src"]
}Running tsc emits JavaScript and a declaration file:
src/index.ts
dist/index.js
dist/index.d.tsThe generated declaration includes exported types and signatures. It omits local implementation details that consumers do not need.
Writing declarations for JavaScript
You can write a declaration file when the implementation is JavaScript or comes from a package without types.
Suppose a package exports a greet function:
// legacy-lib.d.ts
declare module "legacy-lib" {
export function greet(name: string): string;
}Now TypeScript can check this import:
import { greet } from "legacy-lib";
const message = greet("Ada");
console.log(message);If the package actually returns a number instead of a string, the declaration is wrong. TypeScript will still trust the declaration, so test the declaration against the real package behavior.
Declarations can also describe non-code imports handled by a bundler:
// assets.d.ts
declare module "*.svg" {
const url: string;
export default url;
}This tells TypeScript that importing an SVG produces a string URL. The bundler still decides how the SVG is loaded.
What belongs in a declaration file
A declaration file can contain interfaces, type aliases, function signatures, classes, and module declarations. It should describe the public API rather than private implementation.
| File | Purpose |
|---|---|
| `index.ts` | Implements runtime behavior |
| `index.d.ts` | Describes the exported API |
| `assets.d.ts` | Adds ambient types for special imports |
When a declaration describes a module by name, declare module creates an ambient module declaration. When a generated declaration accompanies JavaScript, it usually uses normal export syntax.
Exercise
Why does adding a function body to a generated .d.ts file fail to provide runtime behavior?
Show answer
A .d.ts file is read only for type information. JavaScript must come from an implementation file such as .js or .ts.