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.
Practice
Solution
Step 1: Understand array type syntax
In TypeScript, to declare an array of a specific type, you can usetype[]orArray<type>.Step 2: Check each option
let nums: number[];usesnumber[], which is correct.let nums: number;declares a single number, not an array.let nums: [number];declares a tuple with one number, not a general array.let nums: Array;is missing the type insideArray<>.Final Answer:
let nums: number[]; -> Option DQuick Check:
Array of numbers = number[] [OK]
type[] or Array<type> for arrays [OK]- Forgetting the brackets [] for arrays
- Using just the type without array syntax
- Confusing tuples with arrays
Solution
Step 1: Recall generic array syntax
The generic syntax for arrays in TypeScript isArray<type>, with angle brackets <>.Step 2: Evaluate options
let names: Array<string>;correctly usesArray<string>.let names: Array[string];uses square brackets instead of angle brackets, which is invalid.let names: string[];is valid syntax but uses the shorthand, not generic syntax.let names: Array();uses parentheses, which is incorrect.Final Answer:
let names: Array<string>; -> Option CQuick Check:
Generic array syntax = Array<type> [OK]
- Using square brackets [] instead of angle brackets <>
- Confusing shorthand and generic syntax
- Using parentheses instead of brackets
let fruits: string[] = ['apple', 'banana', 'cherry']; console.log(fruits[1]);
Solution
Step 1: Understand array indexing
Arrays in TypeScript (and JavaScript) start at index 0. So,fruits[0]is 'apple',fruits[1]is 'banana'.Step 2: Identify the logged value
The code logsfruits[1], which is 'banana'.Final Answer:
banana -> Option BQuick Check:
Index 1 in array = banana [OK]
- Thinking array indexes start at 1
- Confusing array elements with their indexes
- Expecting the whole array instead of one element
let scores: Array<number> = [10, 20, '30'];
Solution
Step 1: Check array type and elements
The array is declared asArray<number>, so all elements must be numbers.Step 2: Identify the wrong element
The element '30' is a string because of quotes, which conflicts with the number type.Step 3: Fix the error
Remove quotes from '30' to make it a number:30.Final Answer:
Remove quotes from '30' to make it a number -> Option AQuick Check:
Array type must match element types [OK]
- Mixing string and number types in typed arrays
- Ignoring type errors in array elements
- Changing array syntax instead of fixing element types
Solution
Step 1: Understand nested arrays
An array of arrays means each element is itself an array. So the type is an array of arrays of booleans.Step 2: Check each option
let matrix: boolean[][];uses shorthand nested arraysboolean[][], which is correct but not the generic syntax.let matrix: Array<boolean>;is a simple array of booleans, not nested.let matrix: Array;uses generic syntax for nested arrays:Array<Array<boolean>>, which is correct.let matrix: boolean[];is a simple array of booleans.Step 3: Choose the generic nested array syntax
Since the question asks for the correct type annotation (generic style),let matrix: Array;is the best fit.Final Answer:
let matrix: Array; -> Option AQuick Check:
Nested arrays = Array<Array<type>> [OK]
- Using single array type for nested arrays
- Confusing shorthand and generic syntax
- Missing one level of array nesting
Ready to prove it?
Pass the test and this pattern moves unknown
