Sub topic 13
Incremental builds and project references
This chapter covers two features that keep tsc fast as a codebase grows: incremental compilation and project references. It assumes you can run tsc with npx, you know the tsconfig.json options from earlier chapters (outDir, rootDir, include, strict), and you know what --noEmit and --watch do. Nothing here is required for a small project. It becomes useful when a full type check takes long enough to slow you down, or when one repository holds several packages that depend on each other.
Why full rebuilds get slow
Without extra settings, every tsc run parses and type-checks every file in the program, even if you changed one line. The compiler keeps no memory between runs. Watch mode avoids this by keeping the program in memory, but it only helps while the watcher process is alive. A fresh CI job or a new terminal starts from nothing.
There are two separate fixes:
- Incremental compilation lets one project remember what it did last time, using a file on disk.
- Project references split a codebase into several projects that build in dependency order, so you rebuild only the parts that changed.
Incremental compilation
Set incremental to true and tsc writes a .tsbuildinfo file after each run. On the next run it reads that file and works out which source files changed. It re-checks those files and any file whose imports depend on a changed file's exported types. It reuses the previous results for the rest.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"incremental": true,
"tsBuildInfoFile": "./.cache/app.tsbuildinfo"
},
"include": ["src"]
}tsBuildInfoFile is optional. If you leave it out, the file goes next to the output: inside outDir when one is set, otherwise beside the tsconfig.json. The file is a build artifact, so add it to .gitignore.
The .tsbuildinfo file records, for each source file, a hash of its contents and a signature of its exported types, plus the import graph between files. A change to a function body inside a file leaves that file's signature the same, so files that import it are not re-checked. A change to an exported type changes the signature, so its importers are re-checked.
Incremental mode also works with --noEmit. This is a common CI setup:
npx tsc --noEmit --incrementalIf your CI system caches the .tsbuildinfo file between runs, the type check only does work for changed files.
Project references
Incremental compilation speeds up one project. Project references let you split a repository into several projects, each with its own tsconfig.json, and declare which ones depend on which. The compiler then treats each project as a unit that can be built, skipped, or cleaned on its own.
The benefits:
- A change in one package does not force a re-check of packages that do not depend on it.
- A project can only see the public output of the projects it references, which enforces boundaries between packages.
- Editors can load projects separately, which keeps large repositories responsive.
An example layout
repo/
tsconfig.json
tsconfig.base.json
packages/
core/
tsconfig.json
src/index.ts
app/
tsconfig.json
src/main.tsThe shared settings go in tsconfig.base.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"skipLibCheck": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
}
}The core project is the dependency. It sets composite, which is the flag that marks a project as referenceable:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}export function add(a: number, b: number): number {
return a + b;
}The app project lists core in references:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [{ "path": "../core" }]
}import { add } from "../../core/src/index.js";
console.log(add(1, 2));The path in a reference points to the other project's directory, or directly to its tsconfig.json.
The rootDir and outDir values are repeated in each project on purpose. Paths in an extended config are resolved relative to the file where they are written. If you put outDir: "dist" in the base file, every project would write into the same dist folder next to the base config.
The root tsconfig.json is a solution file. It compiles nothing itself and only lists the projects:
{
"files": [],
"references": [
{ "path": "packages/core" },
{ "path": "packages/app" }
]
}The empty files array matters. Without it, the root config would use the default include and try to compile every .ts file under the repository.
What `composite` changes
Setting composite: true applies stricter rules so that other projects can rely on the output:
declarationdefaults totrue, so each project emits.d.tsfiles. Referencing projects read these instead of your source.incrementaldefaults totrue, so the project writes a.tsbuildinfofile.rootDirdefaults to the directory holding thetsconfig.json, and every source file must be matched byincludeorfiles. A file that imports something outside the project's file list is an error.- A referenced project cannot turn off emit. Setting
noEmitin a project that others reference produces an error.
When app imports from core, the compiler does not type-check core's source again. It substitutes the .d.ts files that core emitted. This is why core must be built first.
Building with `tsc --build`
Plain tsc does not follow references when it builds. You need build mode, written tsc --build or tsc -b:
npx tsc -bRun from the repository root, this reads the solution file and builds core first, then app. If you run tsc -b packages/app instead, it still builds core first because app references it. A later run skips any project whose inputs have not changed since its outputs were written.
| Command | What it does |
|---|---|
| `npx tsc -b` | Builds out-of-date projects in dependency order |
| `npx tsc -b --verbose` | Same, and logs why each project is built or skipped |
| `npx tsc -b --dry` | Shows what would be built without building it |
| `npx tsc -b --force` | Rebuilds everything, ignoring up-to-date checks |
| `npx tsc -b --clean` | Deletes the outputs of the listed projects |
| `npx tsc -b --watch` | Rebuilds affected projects when files change |
The build flow looks like this:
To see the skipping in action, run npx tsc -b --verbose twice. The second run reports both projects as up to date. Then change the body of add in core and run it again. core is rebuilt. Whether app is rebuilt or only has its timestamps refreshed depends on whether the emitted declarations changed. Change the signature of add instead, and app is re-checked against the new .d.ts, so any call that no longer matches becomes a compile error.
Common errors and limits
- Error: "Referenced project must have setting composite: true". Add
composite: trueto the project being referenced. - Error: file is not listed within the file list of project. Under
composite, an imported file that is not matched byincludeorfilesis rejected. Widenincludeor fix the import. - Stale output after a branch switch. Build mode decides what is out of date by comparing inputs with outputs and the
.tsbuildinfodata. If outputs look wrong, runnpx tsc -b --force, or--cleanand then build again. - Circular references. Project A cannot reference B if B references A. Move the shared code into a third project that both reference.
- Cached
.tsbuildinfoin CI. If you cache it, also cache thedistfolders it describes. The info file can claim a project is up to date while its output files are gone. - Declaration maps. With
declarationMap: true, "go to definition" in an editor jumps to the.tssource in the referenced project instead of the.d.tsfile. This is why the base config enables it.
Choosing an approach
Use incremental alone when you have one project and want faster repeated runs, especially with --noEmit in CI. Add project references when you have separate packages with real dependency edges, or when one tsconfig covers code with different settings, such as source and tests, or Node and browser code. For a small single-package project, neither is needed. Keep one tsconfig.json and use --watch during development.
Exercise
A repository has packages/lib and packages/cli, and cli imports from lib. List the changes needed so that npx tsc -b at the root builds lib before cli, and skips lib when only cli changed.
Show answer
Set composite: true in the tsconfig of both packages (at minimum in lib, which is referenced). Add "references": [{ "path": "../lib" }] to packages/cli/tsconfig.json. Add a root tsconfig.json with "files": [] and references to both packages. Run npx tsc -b. Build mode builds lib first because cli references it, and on later runs it skips lib when none of its inputs changed.
Exercise
You run npx tsc in the root of a solution-style repository whose root tsconfig has "files": [] and a references list. Nothing is built. Why, and what do you run instead?
Show answer
Plain tsc does not follow references, and the root config lists no files, so there is nothing for it to compile. Run npx tsc -b, which reads the references and builds each project in dependency order.
Exercise
Why do the core and app configs in the example each set outDir instead of setting it once in tsconfig.base.json?
Show answer
Paths in an extended config are resolved relative to the file that contains them. A single outDir: "dist" in the base file would point both projects at the same dist folder beside the base config, so their outputs would collide.