Sub topic 1
Where the Time Goes: The Phases of a `tsc` Run
Prerequisites
Assume you can write TypeScript with interfaces, generics, and union types. You should also know tsconfig.json, npm, and the tsc command.
Where the time goes
TypeScript’s compiler, tsc, does more than check types. A typical run has four important phases:
- Parse reads
.tsand declaration files and builds syntax trees. - Bind connects declarations to names, such as variables, functions, and modules.
- Check compares types, resolves generics, and reports type errors. Complex types usually slow this phase.
- Emit generates JavaScript and declaration files. With
--noEmit, this phase is skipped.
flowchart LR
A[Parse] --> B[Bind]
B --> C[Check]
C --> D[Emit]Measure first: reading `--extendedDiagnostics`
Start with a repeatable baseline:
npx tsc --noEmit --extendedDiagnosticsThe output includes file counts, memory use, and timings such as Parse time, Bind time, Check time, and Emit time. Compare these values before and after one change.
A high parse time often points to too many source or declaration files. A high bind time can indicate a very large program. A high check time suggests expensive type relationships, broad unions, or deep generic instantiation. Emit time matters when you produce output, but --noEmit removes it from the test.
Do not optimize the largest number automatically. Record the command, configuration, TypeScript version, and whether the run was warm or cold.
Checking too much: finding unintended files and type packages
The compiler’s program is the complete set of files and global type packages it loads. Inspect it:
npx tsc --noEmit --listFilesOnly | grep -v node_modules | wc -l
npx tsc --noEmit --explainFiles > explain.txt--listFilesOnly counts included files. --explainFiles records why each file was included, such as an import, an include pattern, or a library reference. Look for generated folders, tests, duplicate packages, and unexpected declaration files.
The types option limits automatically included global type packages. Narrowing include to src can also reduce work. Check these changes carefully: excluding a file can hide real errors or remove required global declarations.
Exercise
Which phase should you investigate first when Check time dominates the diagnostic output?
Show answer
Investigate the type relationships and generic instantiations processed during checking, then use a trace to locate the expensive code.