What are intersection types in TypeScript?
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.