Challenge 2: Set Up an Incremental Build — Possible Solution ==================================================================== // tsconfig.json { "compilerOptions": { "target": "ES2022", "module": "commonjs", "strict": true, "outDir": "./dist", "rootDir": "./src", "incremental": true, "tsBuildInfoFile": "./.cache/tsbuildinfo.json" }, "include": ["src/**/*.ts"] } // .gitignore should include the cache directory: // .cache/ WHAT CHANGES ON THE SECOND tsc RUN ----------------------------------- - FIRST RUN: tsc has no prior information, so it type-checks every file in the project from scratch. It then writes a build-info file (here, ./.cache/tsbuildinfo.json) recording, for every source file, things like its content hash, which other files it depends on, and a summary of what was already verified. - SECOND RUN (with no changes): tsc reads the existing .tsbuildinfo file, compares each file's current content hash against what's recorded, and since nothing changed, it can skip re-checking files it already knows are unchanged and unaffected — the run is dramatically faster than the first, often by an order of magnitude on a large project. - SECOND RUN (after editing one file): tsc uses the dependency information in .tsbuildinfo to figure out not just that the edited file changed, but which OTHER files import it (and therefore might be affected by the change). Only that edited file plus its dependents get re-checked — files completely unrelated to the change are skipped entirely. - Without "incremental": true, every single `tsc` invocation re-checks the entire project from a cold start every time, regardless of how small the actual change was — this is the behavior incremental builds are specifically designed to avoid. WHY THIS WORKS -------------- - "incremental": true is the switch that tells tsc to write and consult a build-info cache at all — without it, "tsBuildInfoFile" has nothing to do. - Pointing tsBuildInfoFile at a dedicated cache directory (rather than leaving it at the default location alongside compiled output) keeps the cache file out of source control and out of the way of the actual build artifacts in "outDir". - This is the single-line change that gives the biggest, easiest performance win mentioned in the chapter — no type simplification or code restructuring required, just telling the compiler to remember what it already checked.