Sub topic 3
Recognizing Expensive Types and Excessive Instantiation
Assume you know TypeScript generics, unions, intersections, and tsconfig.json. This chapter focuses on checker time after you have measured a slow project.
Find expensive type work
The checker may repeatedly instantiate a generic type. Instantiation means creating a concrete type from a generic type and its arguments. Deep conditional types, recursive mapped types, and large unions can cause this work to grow quickly.
A trace can reveal the location:
npx tsc --noEmit --generateTrace traceDir
npx @typescript/analyze-trace traceDirLook for repeated operations, excessive depth, or one type that dominates checking time. A slow type often appears in many diagnostics or inference paths.
Rewrite types to reduce work
Prefer named, reusable shapes over repeatedly computed intersections. Explicit annotations also limit how much TypeScript must infer and print.
interface User {
id: string;
}
interface Permissions {
canDelete: boolean;
}
interface AdminUser extends User, Permissions {}
export function createAdmin(id: string): AdminUser {
return { id, canDelete: true };
}This does not make every interface faster than every intersection. Measure the change. For deep conditional types, split one large type into named intermediate types, narrow unions earlier, and avoid recursive definitions when a simpler model is sufficient.
> callout tip > Simplifying a type can change its behavior. Preserve the public API, then compare diagnostics and compile times before and after the change. >
When only the editor is slow
If command-line checking is fast but navigation or completion is slow, investigate tsserver, the TypeScript language service used by editors. Enable verbose logging in the editor or start the server with a log file:
npx tsserver --logVerbosity verbose --logFile tsserver.logInspect requests that take a long time, repeated project loading, and unexpectedly large projects. Also check whether the editor uses the workspace TypeScript version and the intended tsconfig.json.
Verify and prevent regressions
Run the same command before and after the change:
npx tsc --noEmit --extendedDiagnosticsCompare Check time, total time, file count, and memory. Keep the smallest change that improves the relevant metric. Record the result in project documentation or continuous integration so a future type change does not silently restore the slowdown.
Exercise
A type simplification lowers editor latency, but Check time is unchanged. What should you investigate next?
Show answer
Check tsserver logs and editor project scope, because the editor may be loading extra files or spending time outside the compiler check measured by the command.