Sub topic 7
Building Project Graphs with `tsc --build`
Assume you know basic TypeScript, can run tsc, and can read tsconfig.json files. This chapter uses a monorepo-style layout with separate core and app packages.
When a codebase grows, one TypeScript project can become slow to rebuild. The compiler must parse, check, and emit many files even when only one package changed. Project references divide the codebase into smaller projects. Build mode then treats those projects as a dependency graph.
How build mode works
A project reference connects one TypeScript project to another. The referenced project must normally use composite: true. Composite mode enables the incremental information that build mode needs and requires the project to have a clear set of input files.
Consider this layout:
packages/
core/
src/
tsconfig.json
app/
src/
tsconfig.jsonThe app package imports code from core, so app references core:
{
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist",
"strict": true
},
"references": [
{ "path": "../core" }
],
"include": ["src"]
}The core project also needs a composite configuration:
{
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist",
"strict": true
},
"include": ["src"]
}The generated declaration files in core/dist describe its public types. While building app, TypeScript can use those declarations instead of checking every core source file again.
Building the graph
Run build mode with the --build or -b flag:
tsc --build packages/appTypeScript reads the reference from app, visits core first, and then builds app. It checks each project's build information to decide whether rebuilding is necessary.
The --verbose flag explains these decisions:
tsc --build packages/app --verboseTypical messages indicate that a project is up to date, that an input file changed, or that an output file is missing. This makes it useful when a build is unexpectedly slow or when a package seems not to rebuild.
You can build several entry points at once:
tsc --build packages/core packages/app --verboseTypeScript combines their references into one graph and avoids rebuilding the same project twice.
Common build commands and errors
| Command | Purpose |
|---|---|
| `tsc -b packages/app` | Build the app and its references |
| `tsc -b packages/app --verbose` | Show skip and rebuild decisions |
| `tsc -b packages/app --clean` | Remove outputs recorded by build mode |
| `tsc -b packages/app --force` | Rebuild projects even if they appear current |
--clean is destructive to generated files. Do not use it on a directory that contains hand-written files. --force is useful after changing compiler settings or build tooling, but it gives up the normal incremental skip.
A reference is a build boundary, not automatically a runtime package boundary. Your package manager still needs to resolve the import, and the built package must expose the files that consumers expect.
When project references help
References work best when packages have stable public interfaces and a clear dependency direction. They reduce repeated checking and let independent parts of a monorepo build separately.
They also add configuration and coordination. Circular references are invalid, and changing a widely used declaration can cause many dependent projects to rebuild. Start with natural package boundaries rather than splitting every directory into a project.
Exercise
If app references core, which project does tsc --build packages/app build first, and why?
Show answer
It builds core first because app depends on core's generated declarations. Build mode follows the reference graph so dependencies are ready before dependents.