Sub topic 1
Where the Time Goes: The Phases of a `tsc` Run
Prerequisites
This chapter assumes that you can write TypeScript with interfaces, generics, and union types. You should also know how to edit tsconfig.json and run tsc with npm or npx.
A slow TypeScript command does not have one single cause. The compiler performs several phases, and each phase responds to different fixes. Measure the phases before changing types or configuration.
The four main phases
A tsc run builds a program from your project files. It then processes that program in a mostly predictable order:
Parse
During parsing, TypeScript reads each source file and creates an abstract syntax tree, or AST. An AST is a structured representation of the code. For example, a function declaration becomes a tree containing its name, parameters, return type, and body.
Parse time grows when the compiler reads more files or when files are unusually large. This phase does not decide whether an expression has the correct type. It only determines whether the source has valid TypeScript syntax and records its structure.
If parse time is high, inspect the number and size of included files before rewriting types.
Bind
During binding, TypeScript connects declarations to names. A declaration is code that introduces a name, such as a variable, class, function, or interface. The binder builds symbol tables so later phases can answer questions such as “Which User does this reference mean?”
Binding usually takes less time than checking. It can still become noticeable in a very large program with many declarations, namespaces, or generated files.
Check
Type checking is often the largest cost. The checker resolves types, compares them, follows imports, evaluates generic instantiations, and reports errors.
Generic instantiation means creating a concrete version of a generic type for specific type arguments. A deeply nested conditional type or a large union can cause the checker to repeat substantial work. A type may look short in the source while expanding into a large internal structure.
Check time can also increase when the program includes many libraries or declaration files. A declaration file is a .d.ts file that describes types without containing the implementation. The checker may need to compare your code with those declarations even though it does not check their implementations by default.
Emit
Emit converts the checked program into output files. Depending on compiler options, this can include JavaScript, declaration files, and source maps.
Emit time is usually separate from check time. A project can type-check quickly but emit slowly if it generates many files or declarations. The noEmit option disables output, but TypeScript still parses, binds, and checks the program.
| Phase | Main work | Typical clue |
|---|---|---|
| Parse | Read syntax and build ASTs | Too many or very large files |
| Bind | Build symbol tables | Many declarations |
| Check | Resolve and compare types | Complex or repeated type computation |
| Emit | Write JavaScript and declarations | Many generated output files |
Measure each phase
Run this from a directory containing your project configuration:
npx tsc --noEmit --extendedDiagnostics--noEmit makes the measurement focus on program analysis rather than writing output. --extendedDiagnostics prints timings and counts, including parse, bind, check, emit, total time, file count, and memory usage.
Compare the phase times rather than focusing only on total time. For example, a high parse time with an unexpectedly large file count suggests a program-scope problem. A high check time with a normal file count suggests expensive types or library checking. A high emit time matters only when your normal build emits files.
The reported Total time includes work outside the four named phase timings, such as starting the process and loading configuration. Therefore, the phase values may not add up exactly to the total.
Common interpretation errors
A large memory value does not automatically mean that checking is slow. Memory pressure can cause slowdowns, but it is a separate signal.
Likewise, a high total time does not identify the cause. Always ask which phase increased. Adding --noEmit can reveal whether output generation is the problem, but it cannot remove parsing or type checking from the run.
In an editor, tsserver uses TypeScript's compiler services instead of running one full command for every keystroke. It may still perform the same kinds of parsing, binding, and checking work repeatedly. A slow editor therefore does not prove that emit is slow. It often points to program size or type-checking cost.
Exercise
A project reports Parse time of 1.2 seconds, Check time of 18 seconds, and Emit time of 0.4 seconds. Which phase should you investigate first, and why?
Show answer
Investigate type checking first. The check phase dominates the measured work, so inspect expensive types, generic instantiations, and included declarations before changing emit settings.