Structured Data

Lesson 5: Arrays, Tuples, and Objects

Store collections of values and structured records more safely by using typed arrays, tuples, and objects in TypeScript.

Typed Arrays

let scores: number[] = [80, 90, 95];
let fruits: string[] = ["Apple", "Banana", "Orange"];

Typed arrays make sure that only the correct type of value is stored inside the collection.

Tuples

let student: [string, number] = ["Ali", 20];

A tuple has a fixed number of elements, and each element can have its own specific type.

Objects

let book: { title: string; price: number } = {
  title: "Learning TypeScript",
  price: 39.99
};

Arrays of Objects

let students: { name: string; score: number }[] = [
  { name: "Ali", score: 88 },
  { name: "Siti", score: 92 }
];

Arrays of objects are common in real applications because they allow you to manage many records efficiently.

Best practice: Use objects when the data has named properties. Use tuples only when position matters strongly.

Summary

Arrays, tuples, and objects are essential building blocks in TypeScript. Strong typing makes these data structures safer and easier to understand.