Sub topic 13
Reusing Build State in CI
This chapter assumes you can run tsc, edit tsconfig.json, and understand incremental builds, .tsbuildinfo files, declaration files, and project references. The goal is to reuse safe build results between CI runs without allowing stale output to hide a source change.
What CI caching can reuse
A continuous integration (CI) job usually starts in a clean workspace. TypeScript must parse, check, and emit the project again even when only one file changed. A cache preserves files from an earlier job so the next job can skip unchanged work.
For TypeScript builds, cache these items when they are stable and reproducible:
.tsbuildinfofiles, which record input versions and build decisions.- Emitted JavaScript and declaration files, such as
dist. - Build outputs for referenced composite projects.
- Package-manager data, such as npm's download cache, separately from TypeScript outputs.
Do not cache the entire workspace blindly. Files such as node_modules can contain platform-specific binaries, and stale generated files can hide configuration mistakes.
Choose a cache key
A cache key identifies the inputs that make a build result valid. Include values that affect emitted output:
- Operating system and CPU architecture when native tools are involved.
- Node.js and TypeScript versions.
- Lockfile contents.
- TypeScript configuration files.
- Source files, if the cache is intended to restore final outputs.
A source hash creates a precise key, but it often produces a new cache for every commit. A dependency-and-toolchain key with restored build state is usually more useful. TypeScript still checks the .tsbuildinfo data and rebuilds projects whose inputs changed.
The cache must never be restored from an unrelated branch or from a newer dependency set. Use a prefix fallback only when the restored files are safe to validate and replace.
A GitHub Actions example
This workflow caches the TypeScript state and output directories. The hash includes source and configuration files, so a changed source tree receives a new exact cache key. The restore prefix can reuse an older cache if one exists.
name: TypeScript build
on:
push:
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- uses: actions/cache@v4
with:
path: |
.cache
packages/*/.cache
dist
packages/*/dist
key: typescript-${{ runner.os }}-node22-${{ hashFiles('package-lock.json', '**/tsconfig*.json', 'src/**/*.ts', 'packages/**/*.ts') }}
restore-keys: |
typescript-${{ runner.os }}-node22-
- run: npx tsc --build --verbosePlace each project's tsBuildInfoFile inside one of the cached directories. For example, a root configuration might use .cache/app.tsbuildinfo. Build outputs should be ignored by version control even though CI caches them.
{
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./.cache/app.tsbuildinfo",
"outDir": "./dist"
}
}Cache correctness and failures
A cache improves speed but must not determine correctness. Always keep a clean build command available. Run it periodically, or run it on protected branches, to detect missing files and invalid assumptions.
The most common errors are:
- Changing
tsconfig.jsonwithout changing the cache key. - Reusing state after changing the TypeScript version.
- Caching only
.tsbuildinfowhile deleting required referenced outputs. - Sharing one state file between parallel jobs.
- Restoring a cache created by a different operating system.
- Treating a cache miss as a build failure.
Project references add another requirement: cache the .tsbuildinfo and output directory for every referenced project, or let tsc --build regenerate the missing dependency.
How the cache affects a build
Exercise
Exercise
Why should a CI cache key change when the TypeScript version changes?
Show answer
The compiler version can change type checking and emitted files, so state produced by one version may be invalid for another.