Arrays & Tuples

Duration: 10 min  •  Difficulty: Medium

Arrays

Ada dua cara menulis tipe array:

main.ts
const skills: string[] = ["Go", "TypeScript", "SQL"];
// atau
const scores: Array<number> = [90, 85, 100];

Tuples

Tuple adalah array dengan jumlah elemen dan tipe data yang sudah ditentukan urutannya secara pasti.

main.ts
// Urutan: [id, name]
let employee: [number, string];
employee = [1, "Raffi"];
// employee = ["Raffi", 1]; // Error! Urutan tidak sesuai.
console.log("ID Pegawai:", employee[0]);