Course contentsShow
← TypeScript
Lesson 291 of 1,7967. ObjectsFree lesson

Nested Object Type Literals

Creating object types where property values are themselves object types.

Nested Object Type Literals

You'll learn how to define objects containing other objects, each with their own typed properties.

What Are Nested Object Types?

Just as a filing cabinet contains folders, and folders contain documents, objects in TypeScript can contain other objects as property values. A nested object type literal is when you define the type of a property to be another complete object type, rather than just a primitive like string or number.

Think of it like an address: a person has an address property, and that address itself has street, city, and zipCode properties. Each level has its own structure.

The Syntax

You write nested object types by placing complete object type literals inside property definitions:

let user: {
  name: string;
  age: number;
  address: {
    street: string;
    city: string;
    zipCode: number;
  };
};

user = {
  name: "Alice",
  age: 30,
  address: {
    street: "123 Main St",
    city: "Boston",
    zipCode: 2101
  }
};

Notice how address isn't typed as string or number—it's typed as its own object literal with { street: string; city: string; zipCode: number; }.

Accessing Nested Properties

TypeScript tracks types through all levels:

let streetName: string = user.address.street; // ✓ Valid
let zip: number = user.address.zipCode;       // ✓ Valid

You can nest as deeply as needed, though too many levels can hurt readability.

Key Takeaway: Nested object type literals let you model hierarchical data structures where property values are themselves objects with their own typed properties, maintaining type safety at every level.