What if you could combine many rules into one without writing them all out?
How intersection combines types in Typescript - Why You Should Know This
Imagine you have two lists of rules for a game, and you want to follow both sets at the same time. Writing down every possible combined rule by hand would be confusing and take forever.
Manually combining rules means repeating yourself, risking mistakes, and missing some combinations. It's slow and hard to keep track of all the details when rules overlap or conflict.
Using intersection types lets you easily merge multiple sets of rules into one. It automatically requires that values meet all conditions, so you don't have to write out every detail yourself.
type A = { name: string };
type B = { age: number };
// Manually create combined type
// type C = { name: string; age: number; }type A = { name: string };
type B = { age: number };
type C = A & B;It makes combining multiple requirements simple and safe, so your code can handle complex data without extra work.
When building a user profile, you might want to combine personal info and account settings into one object that must satisfy both sets of rules.
Manual merging of types is slow and error-prone.
Intersection types combine multiple types into one automatically.
This helps write clearer, safer code with less effort.
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
