How intersection combines types in Typescript - Performance & Efficiency
We want to understand how the time it takes to combine types using intersection grows as the types get bigger.
Specifically, how does the process of merging two types scale when their properties increase?
Analyze the time complexity of the following TypeScript type intersection.
type A = { a: string; b: number; c: boolean };
type B = { b: number; d: string; e: boolean };
type C = A & B;
This code creates a new type C by combining types A and B using intersection.
When combining types, the system checks each property in both types.
- Primary operation: Comparing and merging properties from both types.
- How many times: Once for each property in both types.
As the number of properties in each type grows, the work to combine them grows roughly by adding their sizes.
| Input Size (properties per type) | Approx. Operations |
|---|---|
| 10 | About 20 checks (10 + 10) |
| 100 | About 200 checks (100 + 100) |
| 1000 | About 2000 checks (1000 + 1000) |
Pattern observation: The work grows linearly as the total number of properties increases.
Time Complexity: O(n + m)
This means the time to combine two types grows in a straight line with the total number of properties in both types.
[X] Wrong: "Combining two types takes time proportional to the product of their sizes (like n * m)."
[OK] Correct: The process just checks each property once per type, so it adds their sizes instead of multiplying.
Understanding how type operations scale helps you reason about code maintainability and compiler performance, a useful skill in real projects.
"What if we combined three types instead of two? How would the time complexity change?"
Practice
A & B represent?Solution
Step 1: Understand intersection type meaning
IntersectionA & Bmeans the type must satisfy both A and B simultaneously.Step 2: Combine properties of A and B
The resulting type includes all properties from A and all from B, so it has everything both define.Final Answer:
A type that has all properties from both A and B -> Option DQuick Check:
Intersection = all combined properties [OK]
- Thinking intersection means either/or properties
- Confusing intersection with union types
- Assuming intersection only keeps common properties
TypeA and TypeB in TypeScript?Solution
Step 1: Recall intersection operator
In TypeScript, the intersection operator is&to combine types.Step 2: Check each option's syntax
type Combined = TypeA & TypeB; uses&correctly. type Combined = TypeA | TypeB; uses union|, C and D use invalid operators for types.Final Answer:
type Combined = TypeA & TypeB; -> Option AQuick Check:
Intersection uses & operator [OK]
- Using | instead of & for intersection
- Using logical operators like && or +
- Confusing syntax with other languages
person?
type Name = { name: string };
type Age = { age: number };
const person: Name & Age = { name: "Alice", age: 30 };Solution
Step 1: Understand intersection of Name and Age
The intersectionName & Agemeans the type must have bothnameandageproperties.Step 2: Check the assigned object
The object hasname: "Alice"andage: 30, matching both types.Final Answer:
{ name: string; age: number } -> Option BQuick Check:
Intersection type has all combined properties [OK]
- Thinking it only has one of the properties
- Expecting a type error for combining
- Confusing with union type behavior
type A = { x: number };
type B = { y: string };
const obj: A & B = { x: 10 };Solution
Step 1: Check intersection requirements
TypeA & Brequires properties from both A and B, so bothxandymust be present.Step 2: Inspect the object literal
The object only hasx: 10but is missingy, so it does not satisfy the intersection type.Final Answer:
Missing property 'y' from type B in the object -> Option CQuick Check:
Intersection needs all properties present [OK]
- Assuming partial properties satisfy intersection
- Thinking intersection merges only common keys
- Ignoring missing properties in object assignment
type Contact = { email: string; phone: string | null };type Personal = { name: string; phone: string };What is the type of
info in this code?const info: Contact & Personal = {
name: "Bob",
email: "bob@example.com",
phone: "123-456"
};Solution
Step 1: Analyze intersection of Contact and Personal
Thephoneproperty isstring | nullin Contact andstringin Personal. Intersection requires a type assignable to both, which isstring.Step 2: Check the object assigned
The object hasname,email, andphoneas a string, matching the intersection type.Step 3: Determine final type
The combined type hasname: string,email: string, andphone: string(the stricter type).Final Answer:
{ name: string; email: string; phone: string } -> Option AQuick Check:
Intersection narrows overlapping types [OK]
- Assuming union of phone types instead of intersection
- Expecting error due to different phone types
- Ignoring type narrowing in intersections
Ready to prove it?
Pass the test and this pattern moves unknown
