What if your program could catch your mistakes before you even run it?
Why Array type annotation syntax in Typescript? - Purpose & Use Cases
Imagine you have a list of your favorite fruits written on paper. You want to tell a friend exactly what kind of list it is, but you just say "it's a list" without saying what goes inside. Later, when your friend tries to add a number or a toy to the list, confusion happens.
Without clearly saying what type of items the list holds, mistakes happen easily. You might accidentally add wrong things, or your program might crash because it expects fruits but gets numbers. It's like mixing apples and socks in the same basket without knowing.
Using array type annotation in TypeScript is like labeling your basket clearly: "This basket holds only fruits." This way, the computer knows exactly what to expect, stops mistakes early, and helps you write safer, clearer code.
let fruits = [];
fruits.push(42); // Oops, added a number by mistakelet fruits: string[] = [];
fruits.push("apple"); // Only strings allowed nowIt lets you create lists that only hold the right kind of items, catching errors before your program runs.
When making a shopping app, you want to store only product names in a list. Using array type annotation ensures you never mix product names with prices or other data by mistake.
Array type annotation tells exactly what kind of items an array holds.
It prevents adding wrong types to your lists.
This makes your code safer and easier to understand.