Sub topic 4
What the `.tsbuildinfo` File Stores and Where It Lives
Assume you know basic TypeScript types, can run tsc, and can edit tsconfig.json. This chapter focuses on the state file used by incremental compilation.
What `.tsbuildinfo` is
When incremental is true, TypeScript writes a .tsbuildinfo file after a successful build. The file records information about that build so the next build can reuse work instead of checking every file again.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"outDir": "./dist",
"incremental": true,
"tsBuildInfoFile": "./.cache/app.tsbuildinfo"
},
"include": ["src"]
}The file is a compiler cache, not a JavaScript or TypeScript module. Its exact structure is an implementation detail and can change between TypeScript versions. Do not import it, parse it in application code, or edit it by hand.
A build commonly records:
- The compiler options that affect the build.
- Each input file's version or content signature.
- Information derived while parsing and checking files.
- Module dependencies between files.
- The output and declaration files associated with inputs.
- Which files may need checking again when one input changes.
TypeScript compares the current project with this saved state. If a source file and its relevant dependencies have not changed, TypeScript can reuse earlier results.
How the cache affects a build
A normal compilation performs several stages:
The cache does not make every build free. TypeScript still reads the project, checks timestamps or signatures, and emits files that need updating. A small edit can still affect many files if it changes an exported type used throughout the project.
A change to compiler options can also invalidate much of the cache. For example, changing strict, target, module resolution, or path mappings may require TypeScript to redo earlier work.
Choosing the file location
You can set an explicit location with tsBuildInfoFile. This is useful because the location becomes predictable:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"incremental": true,
"tsBuildInfoFile": ".cache/tsconfig.tsbuildinfo"
},
"include": ["src"]
}The parent directory must exist or be creatable by the build environment. Keep the state file outside published package contents unless consumers need it, which they normally do not.
Without tsBuildInfoFile, TypeScript chooses a location based on the project configuration. The default is generally near the emitted output, while composite projects commonly place the file beside the project configuration. The exact path can vary with options such as outDir, outFile, and composite, so an explicit path is safer for scripts and CI.
| Location | Benefit | Risk |
|---|---|---|
| `.cache/tsconfig.tsbuildinfo` | Predictable and easy to ignore | Must preserve the cache directory |
| `dist/tsconfig.tsbuildinfo` | Stays with build output | Cleaning `dist` removes the cache |
| Repository root | Easy to find | Can clutter the project |
Cleaning and sharing the state
Add local compiler state to .gitignore:
.cache/
*.tsbuildinfoDelete the file when debugging a suspicious build, changing TypeScript versions, or changing configuration in a way that seems to leave stale output. A missing or invalid state file is safe: TypeScript performs a full build and creates a new one.
In continuous integration, cache .tsbuildinfo together with the relevant build outputs. Key that cache by the operating system, Node.js version, TypeScript version, lockfile, and configuration. Restoring a cache from an unrelated toolchain can reduce its usefulness or trigger a full rebuild. It should never affect correctness.
Exercise
Why should a CI cache key include the TypeScript version?
Show answer
The saved format and compiler behavior can change between TypeScript versions. A cache created by another version may be unusable or may cause unnecessary rebuilding, so the version should distinguish cache entries.