TypeScript Object Types

Describe the shape of objects.

✍️ 3 exercises + code challenge in this lesson

Object types list each property and its type:

const car: { brand: string; year: number } = { brand: "Toyota", year: 2020 };

  • Optional property: color?: string
  • Read-only property: readonly id: number
  • Index signature for dynamic keys: { [key: string]: number }

Key Takeaways: TypeScript Object Types

Mastering TypeScript Object Types helps you write clean and reliable TypeScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

TypeScript Object Types Code Example

TypeScript
const car: { brand: string; year: number; color?: string } = {
  brand: "Toyota",
  year: 2020,
};

const stock: { [item: string]: number } = { Laptop: 12, Mouse: 150 };

console.log(car.brand, car.color ?? "no colour", stock.Mouse);
Output
Toyota no colour 150
Free sign-in required · Google login available

TypeScript Object Types Practice Exercises

Check what you learned in “TypeScript Object Types”.

/ 3 solved
Fill in the blank

Make the color property optional.

let car: { brand: string; color: string };
✓ Correct! Not quite. Try again.
Multiple choice

Which modifier prevents a property from being changed?

✓ Correct! Not quite. Try again.
Code challenge

Create book: { title: string; pages: number } = { title: "Clean Code", pages: 464 } and log book.title.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services