0
0
Typescriptprogramming~10 mins

How intersection combines types in Typescript - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How intersection combines types
Type A
Intersection Type
Combined properties of A and B
Intersection takes two types and creates a new type that has all properties from both types.
Execution Sample
Typescript
type A = { name: string };
type B = { age: number };
type C = A & B;

const person: C = { name: "Alice", age: 30 };
Defines two types A and B, then combines them with intersection to create type C having both properties.
Execution Table
StepActionEvaluationResult
1Define type AA = { name: string }Type A created with property name:string
2Define type BB = { age: number }Type B created with property age:number
3Create intersection type C = A & BC has properties of A and B combinedC = { name: string, age: number }
4Declare variable person of type Cperson must have name:string and age:numberperson = { name: "Alice", age: 30 }
5Access person.nameperson.name is string"Alice"
6Access person.ageperson.age is number30
7Try to assign person missing ageError: age missingTypeScript error
8Try to assign person missing nameError: name missingTypeScript error
💡 Execution stops after type errors prevent invalid assignments
Variable Tracker
VariableStartAfter 1After 2After 3Final
personundefinedundefinedundefined{ name: "Alice", age: 30 }{ name: "Alice", age: 30 }
Key Moments - 3 Insights
Why does person need both name and age properties?
Because the intersection type C combines A and B, person must have all properties from both types, as shown in execution_table row 3 and 4.
What happens if we try to create a person without one property?
TypeScript shows an error because the intersection requires all properties, as seen in execution_table rows 7 and 8.
Does intersection merge properties or overwrite them?
It merges properties from both types, so the resulting type has all properties combined, as in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what properties does type C have at step 3?
A{ name: string }
B{ name: string, age: number }
C{ age: number }
D{}
💡 Hint
Check execution_table row 3 under Result column
At which step does TypeScript show an error for missing properties?
AStep 7
BStep 4
CStep 5
DStep 6
💡 Hint
Look at execution_table rows 7 and 8 for errors
If type B also had a property name:string, what would happen to type C?
AC would have one name property from A only
BC would have two name properties causing error
CC would have name:string property from both merged as one
DC would ignore name property
💡 Hint
Intersection merges properties; duplicate property types merge as one
Concept Snapshot
Intersection types combine multiple types into one.
Syntax: type C = A & B;
Result: C has all properties from A and B.
Variables of type C must have all combined properties.
Missing any property causes a TypeScript error.
Full Transcript
This visual execution shows how TypeScript intersection types combine two types into one. First, type A with property name:string and type B with property age:number are defined. Then, type C is created as the intersection A & B, which means C has both name and age properties. A variable person of type C must have both properties. Accessing person.name and person.age works as expected. Trying to assign person without one of these properties causes a TypeScript error. Intersection merges properties from both types, requiring all to be present.