Sub topic 3
Turning on incremental builds
Assume you know basic TypeScript types, can run tsc, and can read a tsconfig.json file. This chapter focuses on improving repeated builds of one TypeScript project. Project references and declaration generation are covered later.
A TypeScript build has several stages. The compiler reads source files, creates an internal representation, checks types, and emits JavaScript or declaration files. A normal build repeats these stages for the whole project, even when you changed only one file.
The incremental compiler option lets TypeScript remember information from an earlier build. On a later build, TypeScript compares the current project with that saved information and reuses work for unchanged files.
Configure incremental compilation
Add incremental: true under compilerOptions:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"outDir": "./dist",
"incremental": true,
"tsBuildInfoFile": "./.cache/app.tsbuildinfo"
},
"include": ["src"]
}The first build still checks the complete project. It also writes a build-information file, commonly called a .tsbuildinfo file. The next build reads that file.
tsBuildInfoFile chooses the file's location and name. Keeping it under .cache separates compiler state from generated JavaScript. Add that directory to .gitignore unless your team deliberately stores it elsewhere.
| Setting | Purpose |
|---|---|
| `incremental` | Reuses information from previous builds |
| `tsBuildInfoFile` | Chooses where the saved state is written |
| `outDir` | Chooses where emitted files are written |
What changes trigger work?
TypeScript tracks facts about project files, including their versions, dependencies, and compiler settings. If a source file changes, TypeScript determines which dependent files might need checking again.
A change to a function body often affects less work than a change to an exported type. For example, changing the implementation of an internal helper may not require rechecking every consumer. Changing an exported interface can affect files that import it.
Incremental compilation does not mean “compile only the file whose timestamp changed.” TypeScript must preserve type safety, so it follows the dependency graph and rechecks files affected by changed types.
Measure the improvement
Measure before and after enabling the option. The --extendedDiagnostics flag prints timing and workload information:
tsc -p . --extendedDiagnostics
# Change one source file, then run the same command again.
tsc -p . --extendedDiagnosticsCompare the first and second runs. Useful values include total time, check time, the number of files, and memory use. Run the command twice without changing files to see the cache's best-case benefit. Then make a small change and measure again.
For a fair comparison:
- Use the same machine and TypeScript version.
- Measure a clean build before enabling
incremental. - Enable
incrementaland measure the first build. - Measure an unchanged second build.
- Measure a build after a typical source change.
Do not delete .tsbuildinfo between measurements. Deleting it forces TypeScript to behave like it has never built the project.
Common errors
An incremental build can appear ineffective for several reasons:
- The build runs in a temporary directory, so the state file disappears.
- A CI job does not restore the cache before running
tsc. - A script deletes
.tsbuildinfoas part of its clean step. - The project is small, so reading the state file costs almost as much as checking the source.
- A compiler upgrade or configuration change invalidates much of the saved state.
The state file is a cache, not a source of truth. If it becomes stale or corrupted, remove it and run a clean build. Do this only when needed because deleting it removes the next build's shortcut.
Exercise
A project has "incremental": true, but every CI build takes the same time as a clean local build. What should you investigate first?
Show answer
Check whether CI restores the .tsbuildinfo file before running tsc and whether the build process deletes it. If CI starts without the state file, every run must perform a full initial build.