🔷 TypeScript Q7 / 101

What is intersection type?

AI-Powered Answer ✓ Answered

An intersection type in TypeScript allows you to combine multiple types into a single type. It takes all the properties from each of the combined types, ensuring that a value of the intersection type must possess all the features of every individual type.

What are Intersection Types?

Intersection types are a powerful feature in TypeScript that enable you to merge the properties of two or more existing types into a new single type. This new type will contain all the members (properties and methods) from all the intersected types.

The & operator is used to define an intersection type. For example, TypeA & TypeB creates a new type that has all the properties of TypeA AND all the properties of TypeB.

Unlike union types (|), where a value can be of one type OR another, an intersection type requires a value to conform to ALL of the types involved simultaneously.

How Intersection Types Work

When you create an intersection of types, TypeScript combines their members. If the types have properties with the same name and compatible types, they are merged. If the types have properties with the same name but different, incompatible types, the resulting type for that property will typically be never.

typescript
interface Person {
  name: string;
  age: number;
}

interface Employee {
  employeeId: string;
  department: string;
}

type EmployeeDetails = Person & Employee;

const john: EmployeeDetails = {
  name: 'John Doe',
  age: 30,
  employeeId: 'EMP001',
  department: 'Engineering'
};

console.log(john);

In this example, EmployeeDetails requires both the name and age properties from Person, AND the employeeId and department properties from Employee. An object of type EmployeeDetails must satisfy all these requirements.

Handling Conflicting Properties

If two types in an intersection have a property with the same name but different non-overlapping types, the resulting property in the intersection type will be of type never. This means no value can ever satisfy that specific property's requirement, effectively making the intersection type unusable if such a property is mandatory.

typescript
interface A {
  value: string;
}

interface B {
  value: number;
}

type Conflicting = A & B;

// const obj: Conflicting = { value: 'hello' }; // Type 'string' is not assignable to type 'never'.
// const obj2: Conflicting = { value: 123 };  // Type 'number' is not assignable to type 'never'.

// The 'value' property in 'Conflicting' type is 'string & number', which resolves to 'never'.

Here, value cannot be both a string and a number simultaneously, so its type becomes never. This correctly indicates that no object can satisfy this definition.

Common Use Cases

  • Combining Mixins: When working with JavaScript mixins, intersection types are perfect for typing the result of applying multiple mixins to a class.
  • Augmenting Existing Types: You can extend an existing type with additional properties without modifying the original type definition, which is useful for third-party libraries.
  • Creating 'All-Inclusive' Types: When you need a type that encapsulates all attributes from several distinct domains, intersection types provide a concise way to express this.