What are intersection types in TypeScript?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid TypeScript basics — a prerequisite for any developer role.
Answer
An intersection type combines multiple types into one using the & operator, creating a type that has all the properties of all combined types. Example: type Employee = Person & JobInfo; — an Employee must have all properties of both Person and JobInfo. This is TypeScript's way of mixing or composing types, similar to how Object.assign() merges objects at runtime. Intersections are commonly used to combine interfaces or add extra properties: type WithTimestamps<T> = T & { createdAt: Date; updatedAt: Date; }. If two intersected types have a property with the same name but incompatible types (e.g., one has id: string, the other has id: number), the resulting property type becomes never (impossible to satisfy). Intersection types (&) create types that have more properties, while union types (|) create types with fewer guaranteed properties.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a TypeScript codebase.