Incremental Compilation: --incremental and .tsbuildinfo
What You'll Learn: How TypeScript can remember previous compilation work to make rebuilds dramatically faster.
The Problem with Repeated Compilation
Every time you run tsc, TypeScript normally analyzes every file from scratch—reading types, checking relationships, and generating output. For large projects, this can take many seconds or even minutes. If you change just one line in one file, TypeScript still rechecks everything.
How Incremental Compilation Works
Think of incremental compilation like a student taking notes during class. Without notes, they'd need to re-read the entire textbook before every exam. With notes, they only review what changed since last time.
When you enable the --incremental flag, TypeScript creates a special .tsbuildinfo file that stores information about your last compilation: which files were checked, what their types were, and how they relate to each other. On subsequent builds, TypeScript reads this cache file and only re-analyzes files that actually changed (or depend on changed files).
Enabling Incremental Compilation
In your tsconfig.json:
{
"compilerOptions": {
"incremental": true,
"outDir": "./dist"
}
}
Or via command line:
tsc --incremental
After the first build, you'll see a .tsbuildinfo file appear (usually in your outDir). This is TypeScript's "memory" between builds. Don't delete it—that's your speed boost!
When It Helps Most
Incremental compilation shines when you're making small changes and rebuilding frequently during development. The first build takes the same time, but subsequent builds can be 2-10x faster depending on what changed.
Key Takeaway: Enable --incremental to cache type information between builds—TypeScript remembers previous work and only rechecks what changed, dramatically speeding up rebuilds in large projects.