Sub topic 7
Recording What the Checker Does with `--generateTrace`
Assume you can run tsc, read tsconfig.json, and use TypeScript generics and union types. This chapter shows how to record the compiler's work when a normal timing summary is not enough.
Why generate a trace?
The --extendedDiagnostics option tells you how long broad compiler phases take. A trace goes further. It records timestamped events from the compiler, including work performed while checking types. You can inspect the trace to find which files, symbols, or type operations deserve attention.
Generate a trace only after measuring a baseline. A trace adds files and takes extra time, so it is a diagnostic sample, not a normal build mode.
npx tsc --noEmit --generateTrace traceDirThis command checks the project described by the nearest tsconfig.json and writes diagnostic data into traceDir. The directory normally contains:
| File | Purpose |
|---|---|
| `trace.json` | Timestamped compiler events that can be inspected by tools |
| `types.json` | Type information referenced by the trace |
If your project has several configurations, select one explicitly:
npx tsc --project tsconfig.json --noEmit --generateTrace traceDirUse a new or empty directory for each investigation. Keeping traces from separate runs together makes comparisons confusing.
Connect the trace to compiler work
A TypeScript compile has several phases. The trace is most useful when the baseline already shows that checking is the expensive phase.
A trace does not automatically prove that a particular type is the root cause. It shows where the compiler spent time during one run. Use it to form a focused hypothesis, then verify that hypothesis with a before-and-after measurement.
Analyze the trace
The TypeScript team maintains @typescript/analyze-trace, a command-line analyzer for trace directories. Run it with npx:
npx @typescript/analyze-trace traceDirThe analyzer summarizes expensive operations and points to source locations or type relationships involved in them. Look for patterns such as:
- The same source location appearing in many expensive operations.
- Very deep type comparisons or instantiations.
- Large unions or intersections being repeatedly compared.
- Generic types being instantiated with many different arguments.
- A declaration file or dependency dominating the trace.
The output is evidence for the next experiment. For example, if one exported function causes a large inferred return type to appear repeatedly, add an explicit return type and measure again. If a type alias expands a long intersection at many call sites, try a named interface or a smaller public type.
A useful investigation loop is:
- Record
--extendedDiagnosticsoutput for the current revision. - Generate a trace for the same revision and configuration.
- Analyze the trace and identify one suspected hot spot.
- Make one small change.
- Run the diagnostics again and compare the relevant phase times.
- Keep the change only if it improves the measured problem without causing type errors.
Handle trace data carefully
Trace files can be large. They can also include absolute file paths and representations of project types. Store them locally and inspect them before uploading them to an issue or sharing them outside your team.
Do not use a trace as a permanent build artifact. Add the trace directory to an appropriate ignore file if your project creates it repeatedly:
traceDir/The trace describes one invocation. A warm incremental build, a clean build, and an editor session can have different causes of slowness. Capture the same kind of run when comparing results.
Exercise
Exercise
Your diagnostics show that checkTime is much larger than parseTime, bindTime, and emitTime. What should you do next, and why should you avoid changing several type definitions at once?
Show answer
Run npx tsc --noEmit --generateTrace traceDir, analyze it with npx @typescript/analyze-trace traceDir, and choose one suspected hot spot. Change one thing at a time so the next measurement shows whether that specific change reduced checking work.