Reading Intersection Error Messages
You'll learn: How to interpret TypeScript's error messages when intersection types fail, helping you debug type conflicts faster.
The Challenge: Deciphering & in Errors
When TypeScript shows you an intersection type in an error message, it uses the & operator to display the combined requirements. Understanding what these messages mean is crucial for fixing type mismatches.
The error message reveals all the constraints your value must satisfy simultaneously.
How TypeScript Shows Intersections in Errors
When you violate an intersection type's requirements, TypeScript will show you the full combined type using &. For example:
Type 'X' is not assignable to type 'A & B & C'
This tells you the value must satisfy properties from A and B and C all at once.
Reading the Combined Requirements
Unlike union types (which offer choices), intersections demand everything. When you see an error like:
Property 'name' is missing in type '{ id: number }'
but required in type '{ id: number } & { name: string }'
TypeScript is telling you: "You gave me id, but the intersection requires both id and name."
Debugging Strategy
When you encounter an intersection error:
- List out each type separated by
& - Check which properties each type requires
- Find the missing or conflicting requirement
- Add or fix the missing pieces
The & symbol in errors is your roadmap showing every obligation your type must fulfill.
Key Takeaway: Intersection error messages use & to show all combined type requirements; reading each component helps you identify exactly what's missing or conflicting.