Sub topic 1
Where TypeScript Build Time Goes
Prerequisites
You should know basic TypeScript types, how to run tsc, how to edit tsconfig.json, and what a .d.ts file contains.
The TypeScript Build Pipeline
The TypeScript compiler usually performs four stages:
- Parse source files into syntax trees.
- Bind names such as variables, functions, and imports.
- Check types across the program.
- Emit JavaScript and declaration files.
Type checking often takes the most time because TypeScript follows imports and compares types across many files. Declaration emit also needs type information unless the project uses isolated declaration rules.
Measuring Before Optimizing
Measure a normal build before changing configuration. The --extendedDiagnostics flag reports file counts and time spent in each compiler stage.
tsc -p . --extendedDiagnosticsRun the command twice after making no source changes. Save both outputs. The comparison shows whether a configuration change reduces checking or emit time.
| Measurement | What it tells you |
|---|---|
| Check time | Time spent validating types |
| Emit time | Time spent writing output |
| Files | How much source the compiler processes |
Turning On Incremental Builds
Incremental compilation saves information from one build and reuses it later. Enable it in tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"outDir": "./dist",
"incremental": true,
"tsBuildInfoFile": "./.cache/app.tsbuildinfo"
},
"include": ["src"]
}On the next run, unchanged files can skip some parsing, checking, and dependency work. A clean build still takes the full cost once.
What `.tsbuildinfo` Stores
The .tsbuildinfo file is compiler state, not a declaration file and not an artifact your application runs. It records information such as compiler options, file versions, dependency relationships, and previously computed results. TypeScript uses this data to decide what must be rebuilt.
Keep it outside source directories, such as .cache, and usually exclude it from published packages. In CI, cache it together with compatible build outputs. Invalidate the cache when TypeScript, compiler options, or important dependencies change.
Exercise
What happens when you enable incremental compilation but delete the .tsbuildinfo file?
Show answer
TypeScript performs a fresh build, then creates the state file again for later builds.