Sub topic 13
Generating `.d.ts` Files with the `declaration` Option
This chapter assumes you know basic TypeScript types and functions, can run tsc, and understand JavaScript import and export statements. You will learn how TypeScript emits declaration files that describe a library's public API.
What a Declaration File Contains
A declaration file has the .d.ts extension. It describes types and exported names without containing runtime implementations. TypeScript uses these files when another project imports a compiled library.
Consider this source file:
// src/index.ts
export interface User {
id: number;
name: string;
}
export function createUser(name: string): User {
return {
id: Date.now(),
name
};
}
export const version = "1.0.0";Compile it with the declaration option:
npx tsc --declaration --emitDeclarationOnly --outDir dist src/index.tsThe --emitDeclarationOnly option asks TypeScript to emit declarations without JavaScript. The generated dist/index.d.ts contains the public type description:
export interface User {
id: number;
name: string;
}
export declare function createUser(name: string): User;
export declare const version = "1.0.0";The function body and the interface's implementation details are absent. The declaration file preserves the information that consumers need for type checking.
Configure Declaration Emit
For a project, put the settings in tsconfig.json:
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"declaration": true,
"outDir": "dist"
},
"include": ["src"]
}Now npx tsc emits both JavaScript and declaration files:
src/index.ts -> dist/index.js
-> dist/index.d.tsUse emitDeclarationOnly when another tool, such as a bundler, produces the JavaScript:
{
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist"
}
}The emitted declarations follow the source folder structure. An exported declaration can refer to another exported type, so TypeScript may emit several .d.ts files and preserve their imports.
The Public API Boundary
Only declarations that are reachable from an exported API need to be understood by consumers. Private implementation details can remain in the JavaScript output.
// src/calculator.ts
interface Options {
rounding: boolean;
}
export function calculate(value: number): number {
const options: Options = { rounding: true };
return options.rounding ? Math.round(value) : value;
}The generated declaration is:
export declare function calculate(value: number): number;Options is not exported and does not appear because it is not part of the function's public type. If an unexported type appears in an exported signature, TypeScript may need to emit that type too. Export types that consumers must name directly.
Common Errors
| Problem | Cause | Fix |
|---|---|---|
| No `.d.ts` files appear | `declaration` is disabled | Set `"declaration": true` |
| JavaScript is missing | `emitDeclarationOnly` is enabled | Remove it when TypeScript should emit JavaScript |
| A private type leaks into an API | An exported signature uses it | Export the type or simplify the signature |
| Consumers cannot find declarations | Package metadata points only to JavaScript | Configure `types` or `exports` in `package.json` |
This process connects source code to the package consumers install:
Exercise
Exercise
Which compiler option prevents TypeScript from emitting JavaScript while still generating .d.ts files?
Show answer
emitDeclarationOnly, used together with declaration: true.