O'Reilly logo

Cross-platform Desktop Application Development: Electron, Node, NW.js, and React by Dmitry Sheiko

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Union and intersection types

Interesting so far, isn't it? What would you say then to it: in TypeScript, we can refer to multiple types at once. For example, we have two interfaces Anakin and Padmé and need a new type (Luke) that inherits from both of them. We can achieve it as easily as this:

interface Anakin { 
  useLightSaber: () => void; 
  useForce: () => void; 
} 
interface Padmé { 
  leaderSkills: string[]; 
  useGun: () => void; 
} 
type Luke = Anakin & Padmé; 

Besides, we can do the intersection without explicitly declaring the type:

function joinRebelion( luke: Anakin & Padmé ){   
}

We can also define a union type that allows any type of a group. You know the jQuery library, right? The function jQuery accepts for a selector parameter a number ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required