Intersection type syntax in Typescript - Mini Project: Build & Apply
Employee with properties id (number) and name (string). Then create another interface called Manager with properties department (string) and reports (number).Use the interface keyword to define each type with the exact property names and types.
EmployeeManager that is the intersection of Employee and Manager using the & symbol.Use type EmployeeManager = Employee & Manager; to combine the two interfaces.
employeeManager of type EmployeeManager and assign it an object with all four properties: id, name, department, and reports. Use these exact values: id: 101, name: "Alice", department: "Sales", reports: 5.Make sure to include all properties from both interfaces in the object.
console.log statement to print the employeeManager object to the console.Use console.log(employeeManager); to display the object.
Practice
What does the intersection type type AB = A & B; mean in TypeScript?
Solution
Step 1: Understand intersection operator &
The&symbol in TypeScript creates an intersection type combining all properties from both types.Step 2: Compare with union and other operations
Unlike union (|), intersection requires all properties from both types to be present.Final Answer:
It combines types A and B so the result has all properties from both. -> Option CQuick Check:
Intersection = all properties combined [OK]
- Confusing intersection (&) with union (|)
- Thinking intersection keeps only common properties
- Assuming intersection excludes properties
Which of the following is the correct syntax to define an intersection type of Person and Employee?
Solution
Step 1: Recall intersection syntax
Intersection types use the&symbol to combine types.Step 2: Check each option
type Staff = Person & Employee;uses&correctly for intersection.type Staff = Person | Employee;uses union|. Comma is invalid syntax.&&is a logical operator, not type syntax.Final Answer:
type Staff = Person & Employee; -> Option BQuick Check:
Intersection syntax = & [OK]
- Using | instead of & for intersection
- Using commas or && which are invalid
- Confusing logical operators with type operators
What is the type of variable user after this code?
type A = { name: string };
type B = { age: number };
const user: A & B = { name: "Alice", age: 30 };Solution
Step 1: Understand intersection type A & B
The typeA & Bcombines properties from both A and B, so it must havenameandage.Step 2: Check the assigned object
The object has bothname: "Alice"andage: 30, matching the intersection type.Final Answer:
{ name: string; age: number } -> Option DQuick Check:
Intersection type = combined properties [OK]
- Thinking intersection keeps only one type's properties
- Expecting a type error when all properties are present
- Confusing intersection with union
Identify the error in this TypeScript code using intersection types:
type X = { a: number };
type Y = { b: string };
const obj: X & Y = { a: 5 };Solution
Step 1: Check required properties for intersection X & Y
The intersection requires all properties from both X and Y, so bothaandbmust be present.Step 2: Inspect the object literal
The object only hasa: 5but is missingb, causing a type error.Final Answer:
Missing property 'b' from type Y in the object. -> Option AQuick Check:
Intersection requires all properties present [OK]
- Forgetting to include all properties in intersection
- Confusing intersection with union allowing partial properties
- Misusing & operator syntax
Given these types, which is the correct way to define a variable fullInfo that must have all properties from Person, Employee, and Contact?
type Person = { name: string };
type Employee = { id: number };
type Contact = { email: string };Solution
Step 1: Understand combining multiple types
To require all properties from Person, Employee, and Contact, use intersection of all three types with&.Step 2: Analyze each option
const fullInfo: Person & Employee & Contact = { name: "Bob", id: 1, email: "bob@example.com" }; correctly usesPerson & Employee & Contact. const fullInfo: Person | Employee | Contact = { name: "Bob", id: 1, email: "bob@example.com" }; uses union which allows any one type only. Mixing union and intersection incorrectly.Final Answer:
const fullInfo: Person & Employee & Contact = { name: "Bob", id: 1, email: "bob@example.com" }; -> Option AQuick Check:
All properties require intersection & [OK]
- Using union | instead of intersection & for all properties
- Mixing union and intersection without parentheses
- Assuming union requires all properties
Ready to prove it?
Pass the test and this pattern moves unknown
