Skip to content

Lesson 3 of 9 · 22m

Incremental State, `.tsbuildinfo`, and Build Orchestration

Use incremental compilation safely, place build state deliberately, and understand why stale type information can be worse than a cold build.

Incremental compilation is seductive because the first benchmark always looks good. Touch one file, rebuild in a second, celebrate. Then CI produces a package built from half-fresh state, an editor uses stale declarations, and the team loses trust in the word "incremental." TypeScript's incremental machinery is solid, but it is state. State has ownership, invalidation rules, and failure modes.

Know which flag does what

There are two overlapping mechanisms:

tsc --incremental
tsc --build

--incremental stores information about the previous compilation in .tsbuildinfo so a subsequent invocation can avoid repeating work. --build orchestrates project references and uses build state per project. In a monorepo, you normally care about build mode because correctness across project boundaries matters more than shaving milliseconds inside one package.

Put state where the lifecycle makes sense

{
  "compilerOptions": {
    "composite": true,
    "tsBuildInfoFile": ".tsbuildinfo/core.tsbuildinfo",
    "outDir": "dist"
  }
}

Build state must share fate with the outputs it describes. If dist is cleaned but .tsbuildinfo survives, TypeScript can believe outputs exist when they do not. Conversely, deleting build state while preserving outputs usually costs performance, not correctness. Build scripts should clean matched pairs.

A boring rule saves teams: either cache both output and state together, or clean both together.

Incremental does not mean formally verified

TypeScript tracks file versions, dependency edges, and semantic diagnostics well enough for real workflows. It cannot know about every side effect in your pipeline. Code generation, postprocessing, asset copying, custom transformers, and package publishing steps need their own invalidation discipline. If a generator changes, downstream type state is only fresh after the generated inputs are fresh.

Orchestrate with explicit commands

Do not hide the graph behind a magical root script until the team understands it:

{
  "scripts": {
    "build": "tsc --build tsconfig.build.json",
    "build:clean": "tsc --build tsconfig.build.json --clean",
    "typecheck": "tsc --build tsconfig.build.json --dry"
  }
}

Dry runs explain what would happen; verbose mode explains why: tsc --build tsconfig.build.json --verbose --dry. Use that output when CI caches misbehave.

CI cache keys should model inputs

A cache key containing only the lockfile is wrong for TypeScript builds. Inputs include source files, configs, generated files, compiler version, and sometimes dependency versions. Many CI systems cannot hash the world cheaply, so choose conservative invalidation: clean build on compiler upgrades, generator changes, and suspicious artifact reuse.

Field notes

  • .tsbuildinfo files are build artifacts. Ignore them unless you have a deliberate artifact-versioning strategy.
  • Incremental typechecking is not a substitute for a clean release build.
  • Watch for filesystem timestamp weirdness in containers and remote builders; cheap CI images have a long history of clock and overlay filesystem surprises.
  • --clean removes outputs known to the build graph, not arbitrary junk you copied there afterward.
  • If the editor and CLI disagree, suspect multiple tsbuildinfo locations or different tsconfig files before suspecting TypeScript itself.

Was this lesson helpful?

Sign in to rate