TypeScript Intersection Types

We can combine many types/interfaces together using & keyword we can get an Intersection Type

The new type will have all members of combined types

interface Person {
name: string;
age: number;
}
interface Employee {
code: string;
}

type Member = Person & Employee;
// Member has all properties of Person and Employee
let peter: Member = {
code: 'PT01',
name: "Peter",
age: 20
}

But when Person and Employee has a same property but with different type -> Typescript will use intersection operator to merge types together. If type is completely incompatible -> never type will be used

interface Person {
name: string;
age: number;
code: number;
}
interface Employee {
name: string[];
code: string;
}

type Member = Person & Employee;
let peter: Member = {
code: 'PT01', // Error TS2322: Type 'string' is not assignable to type 'never'.
name: 'Peter', // TS2322: Type 'string' is not assignable to type 'string & string[]'.
age: 20,
};