Object Literal Types

What

Object literal types allow you to describe the exact “shape” or structure of a JavaScript object by defining the names and types of its properties.

How

You can define an object type directly in a function signature or, more commonly, as a reusable type alias:

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

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

Why

Typing objects provides immediate feedback in your editor. If you misspell a property name or provide the wrong data type, TypeScript will show a compilation error. This prevents the common JavaScript issue where accessing a misspelled property silently returns undefined.

Summary

Defining object shapes is one of the most impactful upgrades TypeScript provides over plain JavaScript.