Sub topic 5
Why Declaration Emit Is Expensive
This chapter assumes you can run tsc, edit tsconfig.json, and read basic TypeScript types and .d.ts files. You should also know that a declaration file describes a module's public API without containing its JavaScript implementation.
A declaration file is not just stripped TypeScript
When TypeScript emits JavaScript, it can remove type annotations and transform syntax. For example, it can turn this source:
export function total(price: number, tax: number) {
return price + tax;
}into JavaScript that keeps the function body:
export function total(price, tax) {
return price + tax;
}A declaration file needs different information:
export declare function total(price: number, tax: number): number;The declaration emitter must know the function's complete public type, including its return type. That type may be written explicitly, or TypeScript may need to infer it from the function body.
This is why declaration emit is a type-analysis task. The compiler cannot safely write a public API until it understands the types used by that API.
How TypeScript finds public types
Consider this example:
// src/format.ts
interface Options {
prefix: string;
}
function makeOptions(): Options {
return { prefix: "$" };
}
export function format(value: number) {
const options = makeOptions();
return `${options.prefix}${value}`;
}The generated declaration might be:
export declare function format(value: number): string;To produce that result, TypeScript needs to inspect the function body and resolve the type of the template expression. In more complex code, the return type may depend on:
- An imported function from another file.
- A generic type parameter.
- A conditional or mapped type.
- An overloaded function.
- The inferred type of an object or array.
- A class whose public members refer to private implementation details.
The compiler therefore parses files, binds names, checks types, and follows relationships between modules before it emits declarations.
Declaration emit can expose hidden dependencies
A function can look local while its exported type depends on another module:
// src/api.ts
import { createUser } from "./users";
export function getUser() {
return createUser("Ada");
}To emit getUser, TypeScript must discover the return type of createUser. If that type refers to another exported type, the declaration emitter must also represent that type correctly and preserve the import relationship.
This work is more expensive than emitting JavaScript because JavaScript does not need to describe the complete static type surface.
A project can also spend time checking code that does not contribute to the final declarations. This often happens when one large project contains application code, tests, generated files, and several libraries. Every file may participate in the same type graph.
Measure the baseline
Use declaration-only emit when you want to measure this part of the build separately:
{
"compilerOptions": {
"strict": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "./dist"
},
"include": ["src"]
}Run the compiler with extended diagnostics:
tsc -p . --extendedDiagnosticsThe output includes time spent parsing, binding, checking, and emitting. A high check time indicates that type analysis dominates. A high emit time indicates that writing output is a larger part of the cost.
Later, project references and isolatedDeclarations can reduce how much work must happen in one compiler process. They do not make type checking unnecessary. They change which work can be reused or performed independently.
Exercise
Why can TypeScript not reliably create a declaration for export function getValue() { return loadValue(); } by simply removing the function body?
Show answer
It must determine the return type of loadValue, preserve any required imports, and write the complete public type of getValue.