Sub topic 4
Declaration Files, Declaration Maps, and Output Directories
Assume you know basic TypeScript types, tsconfig.json, outDir, and ES module imports. This chapter focuses on what a referenced project emits and what a dependent project consumes.
What a declaration file contains
A declaration file is a .d.ts file that describes JavaScript code without including its implementation. TypeScript uses it to type-check code that imports a compiled project.
For example, this source file:
// packages/core/src/greet.ts
export function greet(name: string): string {
return `Hello, ${name}!`;
}produces a declaration file similar to this:
// packages/core/dist/greet.d.ts
export declare function greet(name: string): string;The declaration keeps the public type information and removes the function body. A project that depends on core can check calls to greet without checking the implementation again.
Configuring emitted files
A referenced project normally enables declarations and chooses separate source and output directories:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}outDir is the directory where TypeScript writes generated files. With this configuration, the output might look like this:
packages/core/
├── src/
│ └── greet.ts
└── dist/
├── greet.d.ts
├── greet.d.ts.map
└── greet.jsThe JavaScript file contains runtime code. The declaration file contains the public type shape. The declaration map connects the declaration back to the original TypeScript source.
| Option | Output or behavior | Main purpose |
|---|---|---|
| `declaration` | `.d.ts` files | Share types with consumers |
| `declarationMap` | `.d.ts.map` files | Let editors navigate to source |
| `outDir` | Places generated files in one directory | Keep `src` clean and define the project boundary |
| `rootDir` | Defines the source tree root | Preserve the expected directory structure |
composite requires declaration-oriented project output. It also requires every source file to be included by files or include, because the build system must know the complete project boundary.
How references use declarations
When app references core, build mode builds core first. TypeScript then uses core's emitted declarations as the contract for app.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [{ "path": "../core" }]
}The dependency sees exported types, but it does not need to re-check every source file in core. This reduces work in large repositories and makes the project boundary explicit.
Why declaration maps matter
Without declaration maps, an editor may take you to a generated .d.ts file when you use “Go to definition.” With declarationMap: true, the editor can map that declaration back to packages/core/src/greet.ts.
Declaration maps improve navigation and debugging. They do not change runtime behavior, and they are usually most useful in development or when publishing source maps with a package.
Exercise
Exercise
Why does a referenced library project emit a .d.ts file, and what does declarationMap add?
Show answer
The .d.ts file gives dependent projects the library's public types without its implementation. declarationMap lets editors navigate from that declaration back to the original TypeScript source.