Type-checking performance
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Identify type constructs that slow the checker.
- Reduce instantiation count with interfaces and shallow types.
- Measure with --extendedDiagnostics and --generateTrace.
Overview
#Why types get slow
Every conditional, mapped and recursive type creates instantiations. A type used in many files multiplies the cost. The checker also has a hard instantiation limit.
Common causes
- Deep recursive types without a depth limit.
- Large unions combined with mapped types.
- Complex types exported and reused everywhere instead of named interfaces.
- Overloaded inference that tries many candidates.
Techniques
- Name a computed type with an interface or a single alias so it is computed once.
- Prefer interfaces for object shapes; they are cached by identity.
- Add a depth counter to recursion.
- Split a huge union into a base plus extensions.
Measuring
tsc --extendedDiagnostics
tsc --generateTrace trace
The first prints instantiation and check time; the second produces a trace for the TypeScript performance tool.
Trade-off
Type precision has a cost. Choose the simplest type that still catches the mistakes you care about.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Explain
Why is a named interface usually cheaper to check than the same shape written inline many times?
Reveal the reference answer
reference.typescriptAn interface has a stable identity, so the checker computes and caches its shape once and compares by reference afterward. An inline object type is a fresh anonymous type at each site, so the checker may re-instantiate and re-compare the same structure repeatedly. Naming the type collapses that work.Authored expected output: A written answer naming identity caching and single instantiation.
Caching by identity is the main reason interfaces are cheaper.
Exercise 2Implement
Rewrite this recursive type so it stops after two levels using a depth parameter. ```ts type Deep<T> = T extends object ? { [K in keyof T]: Deep<T[K]> } : T; ```
starter.typescripttype Deep<T> = T extends object ? { [K in keyof T]: Deep<T[K]> } : T;Reveal the reference answer
reference.typescripttype Limited<T, D extends 0 | 1 | 2 = 2> = D extends 0 ? T : T extends object ? { [K in keyof T]: Limited<T[K], D extends 2 ? 1 : 0> } : T;Authored expected output: Compile-time: Limited stops after the declared depth. No runtime output.
Carrying a depth parameter bounds the number of instantiations.
Exercise 3Predict
Which command reports instantiation counts and check time?
Common misconceptions
#- Types cost nothing at runtime, so they cost nothing at all.
- Interfaces and type aliases have identical performance.
- The instantiation limit can be raised safely without bound.
Follow-up practice
#- typescript-assertion-functions
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.