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

What Are Object Type Literals

Understanding the inline syntax for describing object shapes with properties and their types.

What Are Object Type Literals

You'll learn: How to describe the exact shape of an object directly in your type annotations using inline syntax.

The Inline Blueprint

In TypeScript, you don't always need to create named types or interfaces. You can describe an object's structure right where you need it using object type literals. Think of it like sketching a quick blueprint on a napkin instead of drawing formal architectural plans—sometimes the inline version is all you need.

An object type literal uses curly braces {} with property names, colons, and their types:

let user: { name: string; age: number };

user = { name: "Alice", age: 30 }; // ✓ Valid
user = { name: "Bob" }; // ✗ Error: missing 'age'

When You'd Use This

Object type literals shine for one-off type descriptions:

function greet(person: { name: string; isAdmin: boolean }): void {
  console.log(`Hello, ${person.name}`);
}

greet({ name: "Charlie", isAdmin: false }); // ✓ Works

You can nest them, use optional properties, and describe complex shapes inline:

let config: { 
  port: number; 
  host?: string; 
  database: { name: string; port: number } 
};

The Trade-off

While convenient for simple cases, deeply nested or repeated object type literals become hard to read. That's where named types (coming later in your journey) become valuable. For now, object type literals give you the power to describe object shapes wherever you need them.

Key Takeaway: Object type literals let you describe an object's structure inline using { propertyName: type } syntax, perfect for quick, one-off type descriptions.