0
0
Typescriptprogramming~10 mins

Intersection type syntax in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Intersection type syntax
Define Type A
Define Type B
Use & to combine
Create variable of combined type
Access properties from both A and B
Use variable safely with all properties
Intersection types combine multiple types into one that has all properties from each type.
Execution Sample
Typescript
type A = { name: string };
type B = { age: number };

const person: A & B = { name: "Alice", age: 30 };

console.log(person.name, person.age);
This code creates a variable with properties from both types A and B using intersection.
Execution Table
StepActionEvaluationResult
1Define type AA = { name: string }Type A created
2Define type BB = { age: number }Type B created
3Create variable person with type A & Bperson = { name: "Alice", age: 30 }person has name and age
4Access person.nameperson.name"Alice"
5Access person.ageperson.age30
6Print person.name and person.ageconsole.log(person.name, person.age)Output: Alice 30
💡 All steps complete, variable person has combined properties from A and B.
Variable Tracker
VariableStartAfter 3Final
personundefined{ name: "Alice", age: 30 }{ name: "Alice", age: 30 }
Key Moments - 2 Insights
Why can person have both name and age properties?
Because person is typed as A & B, it must have all properties from both types, as shown in execution_table step 3.
What happens if person misses one property from A or B?
TypeScript will give an error because intersection requires all properties, so missing any property breaks the type, as implied by step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what properties does person have?
AOnly name
BBoth name and age
COnly age
DNeither name nor age
💡 Hint
Check the 'Result' column in step 3 of execution_table.
At which step do we see the output printed to the console?
AStep 4
BStep 5
CStep 6
DStep 2
💡 Hint
Look for console.log action in execution_table.
If we remove age from person in step 3, what will happen?
ATypeScript error because age is missing
BNo error, code runs fine
Cperson will have age as undefined
Dperson will only have name property
💡 Hint
Refer to key_moments about missing properties in intersection types.
Concept Snapshot
Intersection types combine multiple types using &.
Syntax: type Combined = Type1 & Type2;
Variables must have all properties from each type.
Useful to merge object shapes safely.
TypeScript checks all properties exist.
Full Transcript
This visual trace shows how intersection types work in TypeScript. First, two types A and B are defined with different properties. Then, a variable person is created with type A & B, meaning it must have all properties from both types. The variable is assigned an object with name and age. Accessing person.name and person.age works because both exist. The console.log prints both values. If any property is missing, TypeScript will give an error. This ensures variables have combined properties safely.