Sub topic 2
Measure first: reading `--extendedDiagnostics`
This chapter assumes you can run tsc, read tsconfig.json, and understand basic TypeScript types. You will measure a build before changing configuration or rewriting types. A measurement gives you a reference point and helps identify which compiler phase is slow.
The phases of a `tsc` run
The TypeScript compiler does more than check whether types are valid. A typical run has these phases:
- Parse reads TypeScript, JavaScript, and declaration files and builds syntax trees.
- Bind connects declarations to names and creates symbol information.
- Check compares types, resolves generic types, and reports type errors.
- Emit writes JavaScript, declaration files, source maps, or other output.
A slow run can have one dominant phase or several moderately slow phases. For example, many included files can increase parse and bind time. Complex conditional types usually increase check time. Large output files or declaration generation can increase emit time.
The phases are related, so a change in one phase can affect another. Do not assume that a type error means checking is the bottleneck.
Run a baseline
From the project directory, run:
npx tsc --noEmit --extendedDiagnostics--extendedDiagnostics prints counts and timings for the compilation. --noEmit tells the compiler to perform its analysis without writing output files. This is useful when you want to measure parsing, binding, and checking without measuring disk output.
Run the command twice if the project uses incremental compilation, and record both results. The first run may include setup costs. For a simple comparison, save the output:
npx tsc --noEmit --extendedDiagnostics > baseline.txtKeep the same command, working directory, TypeScript version, and configuration when comparing a later result. Otherwise, you may measure different programs.
Read the important measurements
The output includes values such as file counts, memory usage, and phase timings. These fields are the most useful starting points:
| Measurement | What it tells you | Typical next question |
|---|---|---|
| Files | How many files belong to the program | Are dependencies or generated files included? |
| Parse time | Time spent reading source text | Are there too many files? |
| Bind time | Time spent creating symbols | Is the program unusually large? |
| Check time | Time spent evaluating types | Are type definitions expensive? |
| Emit time | Time spent writing output | Is declaration or JavaScript generation slow? |
| Total time | Overall compiler time | Did the change improve the whole run? |
The exact values depend on your machine. Compare relative changes rather than using a universal threshold. A check time of 4 seconds may be acceptable for one project and unacceptable for an editor workflow in another.
Memory usage also matters. A high memory value can cause garbage collection and make total time unstable. Record it when a change reduces time but increases memory sharply.
Interpret the result
Start with the largest phase. If check time dominates, investigate type complexity and later generate a compiler trace. If parse time dominates, inspect the number and size of included files. If emit time dominates, check whether you need declaration files, source maps, or generated output for this command.
--noEmit changes the measurement because it removes most output work. That is intentional when your question is “How expensive is type checking?” For a complete build measurement, run the same command without --noEmit and compare the emit time separately.
A useful record includes the TypeScript version, operating system, command, configuration file, file count, phase timings, total time, and memory. This prevents an apparent improvement from coming from a changed dependency installation or a different tsconfig.json.
Exercise
A project reports Parse time: 1.2s, Bind time: 0.4s, Check time: 18.7s, and Emit time: 0.2s. Which phase should you investigate first, and why?
Show answer
Investigate the check phase first because it dominates the measured time. Complex type relationships, conditional types, or repeated generic instantiations may be responsible.