Close

TypeScript - Intersection Types

[Updated: Oct 26, 2018, Created: Oct 26, 2018]

In TypeScript, an intersection type combines multiple types into one.

Example

interface Person {
    name: string;
}

interface Contact {
    phone: string;
}

function showPersonContact(personContact: Person & Contact): void {
    console.log(personContact)
}


let personContact: Person & Contact = {name: "Dane", phone: "111-111-111"};
showPersonContact(personContact);

Output

{ name: 'Dane', phone: '111-111-111' }

The intersection types can also be used for interface/class members:

interface Person {
    name: string;
}

interface Contact {
    phone: string;
}

interface PersonDetail {
    detail: Person & Contact;
}

let personDetail: PersonDetail = {detail: {name: "Dane", phone: "111-111-111"}};
console.log(personDetail);

Output

{ detail: { name: 'Dane', phone: '111-111-111' } }

Example Project

Dependencies and Technologies Used:

  • TypeScript 3.1.3
TypeScript - Intersection Types Select All Download
  • typescript-intersection-types
    • intersection-type-example.ts

    See Also