Sub topic 6
Splitting a Large Codebase with Project References
Project references divide one TypeScript program into smaller projects. This chapter assumes you can write TypeScript with interfaces, generics, and unions, and that you know how to use tsconfig.json, npm, and tsc.
A single tsconfig.json makes the compiler consider every source file together. That is simple, but it becomes costly as the codebase grows. A change in one package can cause TypeScript to recheck unrelated packages. Project references create explicit boundaries. A project consumes the generated declaration files of another project instead of checking all of its source files again.
The project graph
Suppose a web application uses a shared core package:
packages/
core/
src/
tsconfig.json
apps/
web/
src/
tsconfig.json
tsconfig.jsonThe web project references the core project. The root configuration acts as a solution configuration and lists the build order.
// tsconfig.json
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./apps/web" }
]
}// packages/core/tsconfig.json
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"outDir": "../../dist/core",
"rootDir": "src",
"strict": true
},
"include": ["src"]
}// apps/web/tsconfig.json
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"outDir": "../../dist/web",
"rootDir": "src",
"strict": true
},
"references": [
{ "path": "../../packages/core" }
],
"include": ["src"]
}The composite option marks a configuration as suitable for project references. It requires the project to have a predictable set of input files and usually works with declaration output. The core project emits .d.ts files. Those files describe its public types and give the web project a smaller interface to check.
The declarationMap option lets editors navigate from a declaration back to the original source. It improves the development experience but is not required for the dependency boundary.
Building the graph
Run the root configuration with build mode:
npx tsc --buildBuild mode, written as tsc -b, reads the reference graph, builds projects in dependency order, and records build information in .tsbuildinfo files. On a later run, it compares timestamps and signatures to decide which projects need work.
Useful commands include:
npx tsc --build --verbose
npx tsc --build --dry
npx tsc --build --clean--verbose explains which projects are up to date and why. --dry shows the planned work without building. Use it when checking whether a reference graph matches your intent. --clean removes outputs produced by build mode, so use it deliberately.
| Configuration | Main effect | Typical cost or trade-off |
|---|---|---|
| `references` | Defines dependency order and boundaries | Requires separate project configurations |
| `composite` | Makes a project buildable as a referenced unit | Imposes stricter configuration rules |
| `declaration` | Exposes a type-level API to dependents | Adds declaration-generation work |
| `incremental` or build mode | Reuses information from previous runs | Stores `.tsbuildinfo` files |
Project references help most when a change affects one small project or when the editor can work against stable declaration output. A clean build still checks every project, so references do not magically remove all type-checking work. They reduce repeated work and make the affected area clearer.
Checking whether the split helps
Measure before and after the change. For a clean comparison, remove generated outputs, run the build with --verbose, and record elapsed time. For an incremental comparison, change one file in packages/core, then run npx tsc --build --verbose again. Check whether only the core project and its consumers rebuild.
A change to a public declaration can invalidate dependents. A change that affects only the implementation may allow consumers to remain up to date. This distinction is why project references work best when package APIs are intentionally small and stable.
Exercise
The web project has a reference to the core project, but tsc --build --verbose rebuilds both projects after every change inside core. What should you inspect first?
Show answer
Inspect whether the change alters core's emitted declarations or public type signatures. Also check that the projects have separate outputs and that the web project is consuming core through the referenced project boundary rather than importing core source files directly.