Arrays are used to store multiple values neatly within a single variable.
In TypeScript, you can specify exactly what type of data an array can hold.
This prevents you from accidentally mixing strings and numbers incorrectly.
Typed arrays are essential for rendering lists in modern frontend frameworks.
The most common way to type an array is by adding [] after the type.
For example, string[] means an array that can only contain strings.
This is very readable and widely used across the web development industry.
let names: string[] = ["Akash", "John", "Jane"];// names.push(10); // Error: Argument of type 'number' is not assignable. names.push("Mike"); // Works perfectly
console.log(names);
Another valid way to declare an array is using the generic Array<type> syntax.
Both type[] and Array<type> do exactly the same thing behind the scenes.
It mostly comes down to personal or team preference on which one to use.
let scores: Array<number> = [95, 80, 100];// scores.push("High"); // Error! scores.push(75); // Perfect!
console.log("Scores:", scores);
Using typed arrays makes your data structures extremely predictable.
Predictable data renders much faster, providing a better user experience.
Search engines reward fast, bug-free websites with higher page rankings.
let isActiveList: boolean[] = [true, false, true];isActiveList.forEach((isActive) => { console.log(isActive); });
How do you define an array that only accepts numbers?
Arrays are incredibly powerful when their contents are strictly typed.
You will use array methods like map and filter constantly in TypeScript.
Next, we will explore a strict variation of arrays known as Tuples!