avatarSlavo_3, July 26, 2022

Intersection Type

Intersection&Union types are one of the ways in which you can compose types

TODO: provide alt

Intersection and Union types are one of the ways in which you can compose types.
It allows us to combine other types.

Suppose that you have two types: UserName and MemberName

 type userName = {
        name: string;
        privileges= string[];
  type MemberName = {
        name: string;
        paidSubscription: Date;

    // The following defines two intersection types:
    type User = guestUser & memberName

  const userOne: User  = {
        name: "Slavo_3",
        privileges: ["admin"].
        paidSubscription: new Date() | string 

When you intersect types, the order of the types doesn’t matter.

As we are doing in the example above, intersection types can be helpful when used with object types.
You can use them with any type

type Numeric = string | number;
type Combinable = number | boolean 
type Universal = Combinable & Numeric; // type number

TypeScript sees that universal as a type number because that is the only intersection we have in the example.

Intersection operator can be used with any type and then builds the intersection of these types.