Sub topic 2
Why Declaration Emit Is Expensive
Assume you know basic TypeScript types, can run tsc, and can edit tsconfig.json. Declaration emit creates .d.ts files that describe a module's public types. To infer those types, TypeScript usually parses, binds, and checks the whole dependency graph before it writes declarations.
This is slower than emitting JavaScript alone because an exported value can depend on types from other files. A change in one module may require TypeScript to re-check its consumers before it can produce correct declarations.
Splitting a Codebase with Project References
A composite project is a TypeScript project designed to work as one unit in a larger build. Enable it with composite: true. A project reference then connects one project to another.
For example, a library package can produce declarations that an application consumes:
{
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"references": [{ "path": "../core" }],
"include": ["src"]
}The application reads core's .d.ts files instead of repeatedly checking core's source. This creates a boundary: implementation changes inside core do not force the application to inspect every implementation detail.
Building Project Graphs with `tsc --build`
Use build mode to follow references in dependency order:
tsc --build packages/app --verboseBuild mode first builds projects that app depends on. It stores build information and checks which inputs changed. Unchanged projects can be skipped. The --verbose option explains whether each project is rebuilt or considered up to date.
What `isolatedDeclarations` Guarantees
isolatedDeclarations requires exported declarations to contain enough explicit type information for a declaration emitter to process each file independently. It does not make type checking unnecessary. It makes declaration generation parallelizable by removing cross-file inference from the emit step.
export function add(a: number, b: number): number {
return a + b;
}
export interface Config {
retries: number;
}
declare function loadConfig(): Config;
export const config: Config = loadConfig();Without the return and variable annotations, a file may require information from other files to determine its public declaration.
Exercise
Why can project references and isolatedDeclarations reduce declaration build time?
Show answer
References let builds reuse project outputs, while isolatedDeclarations lets declaration emitters process files independently and in parallel.