What is the basic syntax to annotate an array of numbers in TypeScript?
You can annotate an array of numbers using <code>number[]</code>. For example: <code>let nums: number[] = [1, 2, 3];</code>
Click to reveal answer
beginner
How do you annotate an array of strings using the generic Array type syntax?
Use the generic form <code>Array<string></code>. For example: <code>let names: Array<string> = ['Alice', 'Bob'];</code>
Click to reveal answer
intermediate
Can you mix types in an array annotation? How?
Yes, by using a union type inside the array annotation. For example: <code>let mixed: (number | string)[] = [1, 'two', 3];</code>
Click to reveal answer
beginner
What is the difference between number[] and Array<number>?
Both mean the same: an array of numbers. number[] is shorthand, and Array<number> is the generic form. Use whichever you find clearer.
Click to reveal answer
intermediate
How do you annotate a two-dimensional array of booleans?
You can write <code>boolean[][]</code> or <code>Array<Array<boolean>></code>. For example: <code>let grid: boolean[][] = [[true, false], [false, true]];</code>
Click to reveal answer
Which of these is a correct TypeScript annotation for an array of strings?
AArray<string>
BBoth B and C
Cstring[]
Dstring()
✗ Incorrect
Both string[] and Array<string> are valid ways to annotate an array of strings.
How would you annotate an array that can hold numbers or strings?
A(number | string)[]
BArray<number | string>
CBoth A and C
Dnumber[] | string[]
✗ Incorrect
Both (number | string)[] and Array<number | string> correctly annotate an array holding numbers or strings.
What does boolean[][] represent?
AAn array of booleans
BA boolean value
CAn array of arrays of numbers
DA two-dimensional array of booleans
✗ Incorrect
boolean[][] means an array where each element is an array of booleans.
Which syntax is NOT valid for annotating an array of numbers?
AArray<number[]>
BArray<number>
Cnumber[]
DBoth A and B
✗ Incorrect
Array<number[]> means an array of arrays of numbers, not a simple array of numbers.
What is the shorthand syntax for Array<string>?
Astring[]
Bstring()
CArray<string[]>
Dstring{}
✗ Incorrect
string[] is the shorthand for Array<string>.
Explain how to annotate an array of mixed types in TypeScript.
Think about how to say 'this array can hold more than one type'.
You got /3 concepts.
Describe the difference and similarity between number[] and Array.
One is shorter, the other uses angled brackets.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is a correct way to declare an array of numbers in TypeScript?
easy
A. let nums: Array;
B. let nums: number;
C. let nums: [number];
D. let nums: number[];
Solution
Step 1: Understand array type syntax
In TypeScript, to declare an array of a specific type, you can use type[] or Array<type>.
Step 2: Check each option
let nums: number[]; uses number[], 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 inside Array<>.
Final Answer:
let nums: number[]; -> Option D
Quick Check:
Array of numbers = number[] [OK]
Hint: Use type[] or Array<type> for arrays [OK]
Common Mistakes:
Forgetting the brackets [] for arrays
Using just the type without array syntax
Confusing tuples with arrays
2. Which of the following is the correct syntax to declare an array of strings using the generic Array type in TypeScript?
easy
A. let names: string[];
B. let names: Array[string];
C. let names: Array<string>;
D. let names: Array();
Solution
Step 1: Recall generic array syntax
The generic syntax for arrays in TypeScript is Array<type>, with angle brackets <>.
Step 2: Evaluate options
let names: Array<string>; correctly uses Array<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 C
Quick Check:
Generic array syntax = Array<type> [OK]
Hint: Generic arrays use angle brackets, not square brackets [OK]
Common Mistakes:
Using square brackets [] instead of angle brackets <>
Confusing shorthand and generic syntax
Using parentheses instead of brackets
3. What will be the output of the following TypeScript code?
let fruits: string[] = ['apple', 'banana', 'cherry'];
console.log(fruits[1]);
medium
A. apple
B. banana
C. cherry
D. undefined
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 logs fruits[1], which is 'banana'.
Final Answer:
banana -> Option B
Quick Check:
Index 1 in array = banana [OK]
Hint: Remember arrays start at index 0 [OK]
Common Mistakes:
Thinking array indexes start at 1
Confusing array elements with their indexes
Expecting the whole array instead of one element
4. Identify the error in this TypeScript array declaration and fix it:
let scores: Array<number> = [10, 20, '30'];
medium
A. Remove quotes from '30' to make it a number
B. Change 'Array<number>' to 'Array<string>'
C. No error, code is correct
D. Change array to 'number[]' syntax
Solution
Step 1: Check array type and elements
The array is declared as Array<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 A
Quick Check:
Array type must match element types [OK]
Hint: Array type enforces element types strictly [OK]
Common Mistakes:
Mixing string and number types in typed arrays
Ignoring type errors in array elements
Changing array syntax instead of fixing element types
5. You want to declare a variable that holds an array of arrays of booleans in TypeScript using the generic Array type. Which is the correct type annotation?
hard
A. let matrix: Array;
B. let matrix: Array<boolean>;
C. let matrix: boolean[][];
D. let matrix: boolean[];
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 arrays boolean[][], 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 A
Quick Check:
Nested arrays = Array<Array<type>> [OK]
Hint: Nested arrays use Array inside Array or double brackets [OK]
Common Mistakes:
Using single array type for nested arrays
Confusing shorthand and generic syntax
Missing one level of array nesting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.