Intersection types let you combine multiple types into one. This helps you create a new type that has all the features of the combined types.
How intersection combines types in Typescript
type NewType = TypeA & TypeB;
The & symbol means intersection.
The new type has all properties from both TypeA and TypeB.
Employee with both name and job properties.type Person = { name: string };
type Worker = { job: string };
type Employee = Person & Worker;C has both a and b properties.type A = { a: number };
type B = { b: string };
type C = A & B;Z combines three types, so it has x, y, and z.type X = { x: boolean };
type Y = { y: boolean };
type Z = X & Y & { z: number };This program creates an Employee type by combining Person and Contact. Then it makes an object with both name and email. Finally, it prints both values.
type Person = { name: string };
type Contact = { email: string };
type Employee = Person & Contact;
const employee: Employee = {
name: "Alice",
email: "alice@example.com"
};
console.log(`Name: ${employee.name}`);
console.log(`Email: ${employee.email}`);If two types have the same property with different types, the intersection will require the property to satisfy both types, which can cause errors.
Intersection types are different from union types (|), which mean either one type or another.
Intersection types combine multiple types into one that has all their properties.
Use & to create intersection types.
They help make sure values meet all combined type requirements.
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
