Sub topic 5
Reusing Work with Incremental Builds
This chapter assumes you can write TypeScript with interfaces, generics, and union types. You should also know how to edit tsconfig.json and run tsc with npm or npx.
An incremental build is a TypeScript build that saves information about its previous run. On the next run, TypeScript uses that information to avoid repeating work for files that have not changed. This can make local rebuilds much faster.
What incremental builds reuse
A normal build starts by reading the project, parsing every source file, binding names, checking types, and emitting JavaScript. An incremental build still performs these phases, but it compares the current project with saved build information.
The saved information includes details such as:
- Which files belong to the program.
- Which files changed since the previous build.
- Which files depend on changed files.
- Information needed to decide whether type checking or emit must run again.
TypeScript writes this information to a .tsbuildinfo file. The file is an implementation detail. Do not edit it by hand.
The first incremental build usually takes about as long as a normal build because there is no previous state to reuse. Later builds benefit when a change affects only part of the program.
Enable incremental builds
Add incremental: true to the compiler options. Choose an output directory for JavaScript and a separate location for the build information.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"strict": true,
"incremental": true,
"tsBuildInfoFile": ".cache/tsconfig.tsbuildinfo"
},
"include": ["src"]
}Run the build twice:
npx tsc
npx tsc --extendedDiagnosticsThe first command creates JavaScript files and the build-information file. The second command reports timing data for the second run. Compare its Check time, Emit time, and Total time with a clean build.
A source change can invalidate more work than expected. For example, changing an exported type can require TypeScript to check files that import it. Changing a private implementation detail usually affects fewer files.
Know when the cache is useful
Incremental builds help most in long-running local development and watch-mode workflows. They can also help continuous integration when a job restores a cache containing the build-information file and the matching dependencies.
The cache is less useful when:
- Every build runs in a fresh workspace.
- The project changes substantially between builds.
- The compiler version changes.
- Compiler options or dependency versions change.
- A clean build is required for release validation.
Do not copy a .tsbuildinfo file between unrelated projects. Its contents describe a particular project configuration. A stale or mismatched cache is normally safe because TypeScript can rebuild, but it can waste time or make results harder to interpret.
Use a clean build to establish a trustworthy baseline:
rm -rf dist .cache
npx tsc --extendedDiagnosticsThen run the build again without deleting .cache:
npx tsc --extendedDiagnosticsThe second run should usually be faster. Record both results rather than judging the improvement from memory.
Incremental builds and project references
Incremental builds cache work inside one project. Project references split a large codebase into several TypeScript projects. The tsc --build command can then rebuild only projects affected by a change.
These features work well together. Each referenced project can have its own build information, so a change in one package does not necessarily force every package to be checked.
A cache does not solve an oversized program. If every source file changes, or if a public type change affects most dependents, TypeScript still has valid work to do.
The build process can be viewed like this:
Verify the improvement
Measure the same command before and after enabling incremental builds. Keep the project state, compiler version, and machine as consistent as possible. A useful comparison includes:
Total timeParse timeBind timeCheck timeEmit time- Whether the run was clean or incremental
If the second run is not faster, inspect whether the cache was written where expected, whether the workspace is recreated each time, and whether each change affects most of the dependency graph. Incremental builds reduce repeated work; they cannot remove work that the change genuinely requires.
Exercise
After deleting the build output and .tsbuildinfo, which run is the clean baseline: the first npx tsc run or the second one?
Show answer
The first run is the clean baseline. It must create the cache before a later run can reuse previous work.