Sub topic 3
Turning on incremental builds
This chapter assumes you can run tsc from the command line, edit a tsconfig.json, and read the phase timings that --extendedDiagnostics prints (parse, bind, check, emit). Here you will turn on the incremental option, see what it saves, and learn the errors that trip people up.
What incremental does
Without incremental, every tsc run starts from nothing. It parses every file, binds symbols, type checks everything, and emits output. Nothing is remembered between runs.
With incremental: true, tsc writes a file at the end of each run. That file is called the .tsbuildinfo file. It records what the compiler knew about your project: each source file, a hash of its contents, and which files depend on which. On the next run, tsc reads that file first. It compares the current files against the recorded state and works out which files changed and which files are affected by those changes. It then re-checks and re-emits only those files.
The saving is mostly in the check phase, which is usually the largest part of a build. Parsing still happens for every file, because tsc has to read the files to know whether they changed. The .tsbuildinfo file is covered in more detail in the next chapter. This chapter focuses on turning the option on and using it correctly.
Enabling it
Add incremental to compilerOptions. This configuration also sets a custom location for the state file, using tsBuildInfoFile.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"outDir": "./dist",
"incremental": true,
"tsBuildInfoFile": "./.cache/app.tsbuildinfo"
},
"include": ["src"]
}If you leave out tsBuildInfoFile, tsc picks a location itself. When outDir is set, the file goes inside outDir, named after your config file, for example dist/tsconfig.tsbuildinfo. When outDir is not set, it goes next to the tsconfig.json. Setting tsBuildInfoFile explicitly is useful when you want to keep the state out of dist, for instance because you delete dist on every clean build, or because you want to cache one known path in CI.
Add the state file to .gitignore. It contains absolute-looking project state and hashes that are specific to your machine's checkout, and committing it only causes merge conflicts.
Measure the effect
Do not assume the option helped. Run the build twice with --extendedDiagnostics and compare.
tsc -p . --extendedDiagnostics
# run it twice: the second run should report less check time
tsc -p . --extendedDiagnosticsOn the first run there is no state file, so tsc does the full job and then writes .tsbuildinfo. Expect the first incremental run to be slightly slower than a non-incremental one, because it also has to serialize the state. On the second run, with no source changes, the "Check time" line should drop sharply and the "Emit time" line should drop to near zero. Now edit one file and run again. The check and emit times should land between the two earlier results, scaled by how many files were affected.
Look at these lines in the output:
| Line | What it tells you |
|---|---|
| Files | How many files are in the program, including `.d.ts` files from `node_modules` |
| Parse time | Reading and parsing source text. Not skipped by `incremental` |
| Check time | Type checking. The main saving from `incremental` |
| Emit time | Writing JavaScript and declarations. Reduced to the changed files |
| I/O Read time | Reading files, including the `.tsbuildinfo` file |
| Total time | Wall-clock time for the whole run |
If Check time does not fall on the second run, see the problems section below.
How tsc decides what to redo
The compiler does not only look at the files you edited. A change in one file can alter the types that other files see. So tsc uses two pieces of information from the state file.
- A signature for each file. This is a hash of the file's exported shape, which is roughly what its declaration file would contain.
- The list of files each file imports.
When you edit a file, tsc recomputes that file's signature. If the signature is unchanged, for example because you only changed a function body, files that import it do not need to be re-checked. If the signature changed, for example because you added a parameter to an exported function, every file that imports it is re-checked, and so on through the chain.
This is why a small body-only change is cheap and a change to a widely imported type is expensive. It also means the savings you see depend on how your code is shaped.
What invalidates the state
The state file is only useful if tsc trusts it. It throws the saved state away and does a full build when any of these change:
- The TypeScript version. State written by one version is not read by another.
- Compiler options that affect type checking or emit, such as
strict,target,module, orlib. - The set of files in the program, when files are added or removed in a way that changes resolution.
- A change to a dependency's
.d.tsfile innode_modules, for example afternpm installupdates a package.
This matters in practice. If a teammate upgrades TypeScript, or you switch branches with different tsconfig.json settings, expect one full-cost run before the savings return.
Common problems
No speedup on the second run. Check that the state file is actually being written. Look in outDir, next to the config, or at your tsBuildInfoFile path. If tsc cannot write there, for example because the directory is read-only, it fails silently on the saving step and every run is a full run. Also confirm that nothing in your scripts deletes the file between runs, such as a clean step that removes .cache.
Stale output after deleting dist. The state file says "this file was already emitted," so if you delete dist but keep .tsbuildinfo, tsc may emit nothing and leave you with no output. Delete both together, or put the state file inside dist so a clean removes both.
rm -rf dist .cache/app.tsbuildinfo
tsc -p .Using --noEmit with incremental. Since TypeScript 4.0 you can combine them, and tsc will still write the state file. This is a good setup for a type-check-only CI step, because the check results are saved even though no JavaScript is produced. If your build tool already emits code and you only use tsc to check types, this combination is the one to use.
Two configs sharing one state file. If two tsconfig files, such as one for src and one for test, point at the same tsBuildInfoFile, they overwrite each other's state and both lose their savings. Give each its own path.
Expecting parse time to shrink. It will not. If parse time dominates your build, incremental is not the main tool. Reduce the file count with tighter include and exclude settings, or skipLibCheck for the check of library declarations.
A short workflow
- Run
tsc -p . --extendedDiagnosticsand note Check time and Total time as the baseline. - Add
"incremental": trueand, if you want control over the location,"tsBuildInfoFile". - Run twice more. Record the second run's numbers.
- Edit the body of one function and run again. Edit an exported type and run again. Compare.
- Add the state file path to
.gitignore.
Exercise
You enable incremental and run tsc twice with no source changes. The second run is no faster, and Check time is the same. Name two causes you would check first.
Show answer
First, confirm the .tsbuildinfo file is being written at the expected location. A read-only directory, a wrong tsBuildInfoFile path, or a clean script that deletes the file between runs would force a full build every time. Second, check whether something invalidates the state between runs, such as a changed TypeScript version, changed compiler options, or a dependency update that altered .d.ts files in node_modules.
Exercise
After enabling incremental, you edit only the body of a non-exported helper function. Do files that import that module need to be re-checked? Explain using file signatures.
Show answer
No. The file's exported signature, the hash of its exported shape, is unchanged, because the body of a non-exported function does not affect it. tsc re-checks and re-emits only the edited file. Importing files are skipped.