Intersection types

When Anders Hejlsberg added intersection types to TypeScript for the first time, he defined them as follows:

"Intersection types are the logical complement of union types. A union type A | B represents an entity that has either type A or type B, whereas an intersection type A & B represents an entity that has both type A and type B."

The following example declares three interfaces named A, B, and C. Then it declares an object named abc, whose type is the intersection type of the interfaces A, B, and C. As a result, the abc object has properties named a, b, and c, but not d:

interface A { a: string } interface B { b: string } interface C { c: string } declare let abc: A & B & C; abc.a = "hello"; // OK abc.b = "hello"; // ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.