Sub topic 3
Emitting Declaration Files and Declaration Maps
This chapter assumes that you know TypeScript types, npm, tsc, and basic import and export syntax. You will configure a library build so that JavaScript consumers receive runnable code and TypeScript consumers receive the library’s public type information.
Why declaration files matter
A declaration file is a file ending in .d.ts. It describes JavaScript without containing its implementation. TypeScript reads this description when another project imports your package.
For example, this source file exports a function and an interface:
// src/slugify.ts
export interface SlugOptions {
separator?: string;
}
export function slugify(
input: string,
options: SlugOptions = {}
): string {
const separator = options.separator ?? "-";
return input
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, separator)
.replace(new RegExp(`^${separator}+|${separator}+$`, "g"), "");
}A library build can produce a declaration file like this:
// dist/slugify.d.ts
export interface SlugOptions {
separator?: string;
}
export declare function slugify(input: string, options?: SlugOptions): string;The declaration contains the function signature and type, but not the function body. A consumer can therefore write type-safe code without receiving your TypeScript source.
Configure the library build
Use a separate output directory so that source files and published files are easy to distinguish. rootDir identifies the source tree, and outDir identifies the generated tree.
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src"]
}The important options are:
| Option | Result |
|---|---|
| `declaration` | Emits `.d.ts` files beside generated JavaScript |
| `declarationMap` | Emits `.d.ts.map` files linking declarations to TypeScript source |
| `sourceMap` | Emits `.js.map` files linking JavaScript to TypeScript source |
| `rootDir` and `outDir` | Preserve the source layout under the build directory |
Run the compiler with:
npx tscA project containing src/slugify.ts might then contain:
dist/
slugify.js
slugify.js.map
slugify.d.ts
slugify.d.ts.mapThe .js file is used at runtime. The .d.ts file is used by TypeScript. The map files help editors and debuggers trace generated files back to the original source.
Keep the public declarations usable
A public function can expose a type declared in another module. That type must remain available to consumers through the generated declarations. Re-export public types from the package entry point:
// src/index.ts
export { slugify } from "./slugify.js";
export type { SlugOptions } from "./slugify.js";The export type syntax makes the intent clear: SlugOptions is needed for type checking, but it has no runtime value.
A declaration map is especially useful when a consumer uses “Go to definition” in an editor. Instead of opening an unreadable generated declaration, the editor can open the original TypeScript source. Do not treat declaration maps as a replacement for declaration files. Consumers still need the .d.ts files to type-check when the source is absent.
Exercise
Exercise
Which compiler option emits the type descriptions that TypeScript consumers read, and which option links those descriptions back to the source?
Show answer
declaration emits .d.ts files, and declarationMap emits .d.ts.map files for editor navigation back to the TypeScript source.