Sub topic 1
Where TypeScript build time goes
Prerequisites
This chapter assumes that you can read basic TypeScript, run tsc from a terminal, and edit tsconfig.json. You should also know that a .d.ts file describes the types exported by a JavaScript or TypeScript module without containing its implementation.
A TypeScript build is more than converting .ts files into JavaScript. The compiler reads source files, builds an internal model, checks types across that model, and optionally writes JavaScript and declaration files. Each stage can take a different amount of time.
The TypeScript build pipeline
A typical tsc build has four important stages:
- Parse: TypeScript reads each file and turns its text into a syntax tree.
- Bind: The compiler connects declarations to names and creates symbol tables. For example, it records which function a particular identifier refers to.
- Check: The compiler compares types, resolves relationships between files, and reports errors.
- Emit: The compiler writes JavaScript, source maps, and possibly
.d.tsdeclaration files.
The stages are connected. Type checking cannot begin until the compiler has parsed and bound the relevant files. Declaration emit also needs type information unless the project uses a mode that makes declarations derivable from individual files.
A small edit can therefore trigger more work than expected. If an edited type changes the public shape of a module, TypeScript may need to re-check files that depend on that module. In a large application, the check stage is often the largest part of the build.
Measuring before optimizing
Do not assume which stage is slow. Measure a representative build with --extendedDiagnostics:
tsc -p . --extendedDiagnosticsThe output includes values such as:
- Files and lines: how much source the compiler read.
- Parse time: time spent reading syntax.
- Bind time: time spent creating symbols.
- Check time: time spent evaluating types.
- Emit time: time spent writing output.
- Total time: the complete compiler duration.
- Memory used: the compiler's peak memory use.
Run the command twice without changing files:
tsc -p . --extendedDiagnostics
tsc -p . --extendedDiagnosticsThese two runs provide a useful baseline, but they do not represent an incremental build yet. They show the cost of a full invocation using the current configuration. Record the results before changing settings so that later comparisons are meaningful.
| Measurement | What it tells you | Common interpretation |
|---|---|---|
| Parse time | Cost of reading source text | Many or very large files |
| Check time | Cost of type analysis | Complex types or broad dependency graphs |
| Emit time | Cost of writing output | Large JavaScript or declaration output |
Common sources of slow builds
The number of files matters, but file count alone does not explain build time. Large type expressions can make checking expensive. Deeply nested conditional types, wide unions, and heavily generic libraries can require substantial analysis.
The dependency graph also matters. A project with many files importing one central type can cause a change in that type to affect a large portion of the graph. Declaration generation adds work because exported types must be resolved accurately before they can be written to .d.ts files.
Editor builds and command-line builds may differ as well. An editor often keeps compiler state in memory, while a new terminal process starts with no in-memory state. This difference is one reason later chapters use persistent build information and project boundaries.
Exercise
Exercise
A build reports 1.2 seconds of parse time, 0.3 seconds of bind time, 8.5 seconds of check time, and 0.4 seconds of emit time. Which stage should you investigate first, and why?
Show answer
Investigate type checking first. Its 8.5 seconds is much larger than the other stages, so complex types or a broad dependency graph are likely contributing most of the build time.