TypeScript · Senior · Question 47 of 75
Why is extending an interface faster for the compiler than an intersection?
- ✓Correct answer: Interface relationships are cached, while intersections are recomputed
- 2Intersections are checked at runtime, while interfaces aren't
- 3Interfaces compile to classes that the engine caches
Explanation
The compiler caches the result of comparing interfaces by name, while an intersection A & B & C has to be normalized and compared structurally every time. In hot paths with large types this is noticeable. The TypeScript performance wiki says so explicitly.