Sub topic 1
Why One `tsconfig` Stops Scaling
Prerequisites
Assume you know basic TypeScript, tsconfig.json, ES modules, and how to run tsc. This chapter uses a repository with packages/core and packages/app.
Why one `tsconfig` stops scaling
A single TypeScript project works well for a small application. In a multi-package repository, it makes every change look global. The compiler may scan and type-check every package, rebuild unrelated code, and produce one large output tree.
Project references split the repository into independently buildable projects. Each project has its own tsconfig.json. A dependent project consumes the referenced project's declarations instead of repeatedly checking all of its source files.
A shared base config with `extends`
Use a base configuration for options that should remain consistent. Each project extends it and overrides only project-specific settings.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true
}
}Save this as tsconfig.base.json at the repository root. A project can then inherit these settings:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}Turning a project into a composite project
A referenced project must set "composite": true. This tells TypeScript that the project participates in build-mode dependency tracking. Composite projects must also include every source file through include or files.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The compiler records project information in a .tsbuildinfo file. Later builds can use that information to skip unchanged work.
Declaration files, maps, and output directories
With "declaration": true, TypeScript emits .d.ts files. A declaration file contains a module's public types without its implementation. Referencing projects use these files as the package boundary.
outDir keeps generated JavaScript and declarations separate from src. Add "declarationMap": true when editor navigation should trace declarations back to their original TypeScript source.
Exercise
Why should a referenced library emit declaration files?
Show answer
The dependent project can type-check against the library's public API without re-checking its source.