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.
type A = { name: string };
type B = { age: number };
type C = A & B;
const person: C = { name: "Alice", age: 30 };| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Define type A | A = { name: string } | Type A created with property name:string |
| 2 | Define type B | B = { age: number } | Type B created with property age:number |
| 3 | Create intersection type C = A & B | C has properties of A and B combined | C = { name: string, age: number } |
| 4 | Declare variable person of type C | person must have name:string and age:number | person = { name: "Alice", age: 30 } |
| 5 | Access person.name | person.name is string | "Alice" |
| 6 | Access person.age | person.age is number | 30 |
| 7 | Try to assign person missing age | Error: age missing | TypeScript error |
| 8 | Try to assign person missing name | Error: name missing | TypeScript error |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| person | undefined | undefined | undefined | { name: "Alice", age: 30 } | { name: "Alice", age: 30 } |
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.