Sub topic 13
Incremental Builds and Project References
Assume you know JavaScript modules, package.json, npm scripts, and the basic tsc commands from earlier chapters. This chapter focuses on projects split into several TypeScript packages, such as a monorepo.
A large project should not recompile every file after each small change. TypeScript supports incremental builds, which save information about previous compilations and reuse it later. Project references build on this idea by describing dependencies between separate TypeScript projects.
Why split a TypeScript project?
A single tsconfig.json works well for a small application. Larger codebases often contain separate projects:
sharedcontains reusable types and functions.serverusesshared.webalso usesshared.
Each project can have its own source directory, output directory, and compiler settings. A project reference tells TypeScript that one project depends on another.
This structure gives the compiler a dependency graph. When shared changes, TypeScript knows that server and web may need rebuilding.
Incremental compilation
With incremental compilation, TypeScript stores build information in a .tsbuildinfo file. The file records which inputs were checked, which outputs were generated, and which files depend on changed declarations.
Enable it in a project configuration:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"incremental": true,
"tsBuildInfoFile": "./dist/.tsbuildinfo"
},
"include": ["src"]
}The first build still checks all relevant files. Later builds can skip files whose inputs have not changed. Keep .tsbuildinfo out of source control unless your team has a specific reason to commit it.
The declaration option emits .d.ts files. A declaration file describes a module's public types without containing its implementation. Referencing projects use these declarations to understand the dependency's public interface.
Project references
A referenced project must normally enable composite. This option makes the project suitable for use in a reference graph. It also requires settings that let TypeScript identify the project's files and outputs.
Consider this layout:
packages/
shared/
src/index.ts
tsconfig.json
server/
src/index.ts
tsconfig.jsonThe shared configuration can be:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"composite": true,
"incremental": true
},
"include": ["src"]
}The server configuration references shared:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"composite": true,
"incremental": true
},
"include": ["src"],
"references": [
{ "path": "../shared" }
]
}The path points to the directory containing the referenced tsconfig.json. The reference does not automatically rewrite runtime module resolution. Your package setup still needs a valid import path and runtime output layout.
Building the dependency graph
Use tsc --build, commonly abbreviated as tsc -b, for referenced projects. Run it from a solution configuration:
{
"files": [],
"references": [
{ "path": "./packages/shared" },
{ "path": "./packages/server" }
]
}Save this as tsconfig.json at the repository root, then run:
npx tsc --build
npx tsc --build --watch
npx tsc --build --clean--build finds projects in dependency order and rebuilds only those that are out of date. --watch keeps monitoring the graph. --clean removes generated outputs for the referenced projects, so use it deliberately.
Common errors
A reference can fail when the dependency does not emit declarations, when composite is missing, or when rootDir and include do not describe the files consistently. Another common mistake is running npx tsc instead of npx tsc --build; the former treats the current configuration as one project and does not orchestrate the reference graph.
Use project references when separate packages have clear boundaries and build times matter. For a small application, one configuration with incremental enabled is usually simpler.
Exercise
Which command should build a repository containing a root solution configuration and several referenced projects?
Show answer
Use npx tsc --build. It follows the references, builds dependencies first, and skips projects that are already up to date.