Arrays
What
Arrays in TypeScript are used to store lists of elements that all share the same type. This ensures that every item in the list adheres to a specific structure.
How
The most common way to declare an array is using the bracket notation (type[]):
function trainJedi(jediKnights: string[]) {
for (let knight of jediKnights) {
console.log(`Training ${knight}...`);
}
}
trainJedi(["Dooku", "Qui-Gon", "Xanatos"]);
Why
Using typed arrays prevents accidental insertion of incorrect data types into a list. It also provides powerful IDE support, as TypeScript knows exactly what properties and methods are available on each element when you iterate through the array.
Summary
Typed arrays are the primary way to manage collections of data safely and predictably in TypeScript.