We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Optional Object Properties

The following is used way more often than most of us would like, but it is incredibly useful. Optional properties can be added to an object type with the ? operator:

type Superhero = {
  name: string;
  strength: number;
  cape?: boolean; // cape is optional
};

That means that the type of .cape is actually boolean | undefined, just like optional function parameters.

Do not go overboard with optional props... require all the fields that should be there! It will make your life easier with far fewer runtime checks that look like this:

function fight(superhero: Superhero) {
  if (!superhero.cape) {
    // contact edna mode
  }
  // do the happy path thing
}

Assignment

After being copied on all emails and being overwhelmed, Support.ai management doesn't want to be copied on all mail all of the time, only some mail sometimes...