Sub topic 8
Finding Hot Spots with `@typescript/analyze-trace`
This chapter assumes you can run tsc, read tsconfig.json, and understand TypeScript interfaces, generics, and union types. You should also know how to generate a compiler trace with --generateTrace.
A TypeScript trace records work performed by the compiler. The raw files are detailed, but they are difficult to read directly. @typescript/analyze-trace summarizes the trace and points to operations that consumed substantial checking time.
Generate a trace
Run the compiler with tracing enabled:
rm -rf traceDir
npx tsc --noEmit --generateTrace traceDir
npx @typescript/analyze-trace traceDirThe first command removes an old trace so that results do not accidentally include previous data. The second command creates a new trace. The third command analyzes it.
The trace directory normally contains files such as these:
| File | Purpose |
|---|---|
| `trace.json` | Timeline events from the compiler |
| `types.json` | Type information associated with those events |
Run the trace against the same command that normally shows the slowdown. For example, if a project uses a particular project file, specify it:
npx tsc --project tsconfig.json --noEmit --generateTrace traceDir
npx @typescript/analyze-trace traceDirA trace measures one compiler run. It does not automatically describe every editor action or every build.
Read the analyzer output
The analyzer reports expensive operations and gives clues about the types involved. Look for repeated or high-duration entries rather than focusing only on a single large number.
Useful questions include:
- Is most time spent checking one source file?
- Does one generic type appear in many expensive operations?
- Are conditional types repeatedly instantiated?
- Does a library declaration file dominate the report?
- Are the same types compared over and over?
A hot spot is a location or operation that consumes an unusually large share of the checking time. It is evidence for where to investigate, not proof that one line alone is responsible.
A common pattern looks like this:
Instantiating type ...
source: src/api.ts
duration: ...The exact output format can change between analyzer versions. Treat names and locations as leads. Then inspect the source file and the types used there.
From a hot spot to a cause
Suppose the analyzer repeatedly points to a type used by an API response:
type Result<T> =
T extends readonly unknown[]
? { items: Result<T[number]>[] }
: T extends object
? { [K in keyof T]: Result<T[K]> }
: T;This recursive conditional type transforms every nested property. It can be useful, but a large object may cause many type instantiations. If the transformed type is used in many places, checking time can grow quickly.
First, confirm that the type is involved in the slow command. Then test a small change, such as using a simpler type at an application boundary:
interface UserSummary {
id: string;
displayName: string;
}
interface UserResponse {
items: UserSummary[];
}The goal is not to remove advanced types automatically. The goal is to reduce unnecessary work while preserving the public behavior of the program.
Connect trace results to compiler phases
A trace complements, rather than replaces, extended diagnostics.
flowchart TD
A[Run tsc with --generateTrace] --> B[trace.json and types.json]
B --> C[Run @typescript/analyze-trace]
C --> D[Find repeated expensive operations]
D --> E[Inspect source and type definitions]
E --> F[Make one targeted change]
F --> G[Run diagnostics and trace again]If check time is high in --extendedDiagnostics, the trace can help explain which types cause that work. If parse time or bind time is high, simplifying a type may not help. Investigate file count, generated files, or program scope instead.
Repeat the measurement after each meaningful change. A slower trace can be misleading if the compared runs use different files, caches, compiler versions, or commands.
Exercise
The analyzer points to a complex conditional type, but extended diagnostics show that parse time is much higher than check time. Should you rewrite the conditional type first?
Show answer
No. The trace finding concerns type checking, but parsing is the larger measured cost. First investigate the number and size of included source files or generated files, then measure again.