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

Object Literal Types

Okay, I know I said unions were my favorite thing, and that's true when comparing TS to Go. But when it comes to the most useful "upgrade" from JavaScript to TypeScript, it's adding types to objects.

Object literal types allow you to describe the shape of an object:

function logSaiyan(saiyan: { name: string; power: number }) {
  console.log(`${saiyan.name} has power level: ${saiyan.power}!`);
  // ...
}

Or, more likely, you'll define the object type first:

type Saiyan = {
  name: string;
  power: number;
};

function logSaiyan(saiyan: Saiyan) {
  console.log(`${saiyan.name} has power level: ${saiyan.power}!`);
  // ...
}

It's so nice to get a little red squiggly line in your editor when you misspell a property name in TypeScript! JavaScript won't fail until you run it...

Assignment

Support.ai is adding an internal email service for communication between workers and customers.