It is a way to define arrays with a specific type using angle brackets, like Array<Type>. For example, Array<number> means an array of numbers.
Click to reveal answer
beginner
How do you declare an array of strings using generic array syntax?
You write <code>let names: Array<string> = ['Alice', 'Bob'];</code> which means <strong>names</strong> is an array that only holds strings.
Click to reveal answer
beginner
What is the difference between string[] and Array<string> in TypeScript?
Both mean the same: an array of strings. string[] is shorthand, and Array<string> is the generic syntax. You can use either based on preference.
Click to reveal answer
intermediate
Can you use generic array syntax with custom types?
Yes! For example, if you have a type Person, you can write Array<Person> to make an array of Person objects.
Click to reveal answer
intermediate
Why use generic array syntax instead of the shorthand?
Generic syntax is clearer when working with complex types or nested generics. It helps readability and consistency in some cases.
Click to reveal answer
Which of the following declares an array of numbers using generic array syntax?
Alet nums: number<> = [1, 2, 3];
Blet nums: number = [1, 2, 3];
Clet nums: Array = [1, 2, 3];
Dlet nums: Array<number> = [1, 2, 3];
✗ Incorrect
Option D correctly uses Array<number> to declare an array of numbers.
What does Array<string> mean in TypeScript?
AAn array of strings
BA function returning a string
CA string variable
DAn array that can hold any type
✗ Incorrect
Array<string> means an array that holds only strings.
Which is the shorthand for Array<boolean>?
Aboolean[]
Bbool[]
CArray<boolean[]>
Dboolean<>
✗ Incorrect
boolean[] is the shorthand for Array<boolean>.
Can you use generic array syntax with a custom type Car?
ANo, only built-in types work
BOnly if Car is a string
CYes, like <code>Array<Car></code>
DOnly with interfaces, not types
✗ Incorrect
Generic array syntax works with any type, including custom types like Car.
Why might you choose Array<Type> over Type[]?
AIt is required by TypeScript
BIt is clearer with complex or nested types
CIt is shorter to write
DIt runs faster
✗ Incorrect
Generic syntax can be clearer and easier to read with complex or nested types.
Explain how to declare an array of numbers using generic array syntax in TypeScript.
Think about how you tell TypeScript what type the array holds using < and >.
You got /4 concepts.
Describe the difference and similarity between string[] and Array<string>.
Both are ways to say 'array of strings' but look different.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is a correct way to declare an array of numbers using generic array syntax in TypeScript?
easy
A. let nums: number = [1, 2, 3];
B. let nums: Array<number> = [1, 2, 3];
C. let nums: Array = [1, 2, 3];
D. let nums: number[] = 123;
Solution
Step 1: Understand generic array syntax
In TypeScript, Array<Type> declares an array holding elements of Type.
Step 2: Check each option for correct syntax and type
let nums: Array<number> = [1, 2, 3]; uses Array<number> with a number array, which is correct. Others have wrong types or assignments.
Final Answer:
let nums: Array<number> = [1, 2, 3]; -> Option B
Quick Check:
Generic array syntax = Array<Type> [OK]
Hint: Use Array<Type> or Type[] for arrays [OK]
Common Mistakes:
Forgetting to specify the type inside Array<>
Assigning a non-array value to an array type
Using Array without type parameter
2. Which of the following is the correct syntax to declare an array of strings using the shorthand syntax in TypeScript?
easy
A. let names: string[] = ['Alice', 'Bob'];
B. let names: Array = ['Alice', 'Bob'];
C. let names: string = ['Alice', 'Bob'];
D. let names: [string] = ['Alice', 'Bob'];
Solution
Step 1: Recall shorthand array syntax
In TypeScript, Type[] declares an array of Type.
Step 2: Validate each option
let names: string[] = ['Alice', 'Bob']; correctly uses string[] for an array of strings. Others are incorrect or incomplete.
Final Answer:
let names: string[] = ['Alice', 'Bob']; -> Option A
Quick Check:
Shorthand array syntax = Type[] [OK]
Hint: Use Type[] for quick array declarations [OK]
Common Mistakes:
Omitting the type inside Array<>
Using tuple syntax instead of array
Assigning array to a non-array type
3. What will be the output of the following TypeScript code?
function firstElement<T>(arr: Array<T>): T | undefined {
return arr[0];
}
const nums: number[] = [10, 20, 30];
console.log(firstElement(nums));
medium
A. Error
B. undefined
C. 10
D. 30
Solution
Step 1: Understand the generic function behavior
The function firstElement returns the first element of the array or undefined if empty.
Step 2: Apply function to given array
The array nums has elements [10, 20, 30], so arr[0] is 10.
Final Answer:
10 -> Option C
Quick Check:
firstElement([10,20,30]) = 10 [OK]
Hint: Generic function returns first array item or undefined [OK]
Common Mistakes:
Assuming function returns last element
Confusing undefined with zero
Thinking generic causes error
4. Identify the error in the following TypeScript code snippet:
let fruits: Array<string> = ['apple', 'banana', 5];
medium
A. The array contains a number instead of a string
B. Array<string> syntax is invalid
C. Missing semicolon at the end
D. Array should be declared as string[]
Solution
Step 1: Check array element types
The array is declared to hold strings only.
Step 2: Identify type mismatch
The element 5 is a number, which violates the string type constraint.
Final Answer:
The array contains a number instead of a string -> Option A
Quick Check:
Array<string> must hold only strings [OK]
Hint: Array type must match all element types [OK]
Common Mistakes:
Mixing types inside a typed array
Ignoring type errors in arrays
Confusing syntax errors with type errors
5. You want to create a function in TypeScript that accepts an array of any type and returns a new array with the same elements repeated twice. Which is the correct generic array syntax for the function parameter and return type?
hard
A. function repeatArray<T>(arr: Array): Array<T>
B. function repeatArray(arr: T[]): T[]
C. function repeatArray<T>(arr: T): T[]
D. function repeatArray<T>(arr: Array<T>): Array<T>
Solution
Step 1: Define generic parameter and array type
The function uses a generic type T and takes an array of T elements.
Step 2: Ensure return type matches input array type
The function returns an array of T, so Array<T> is correct for both parameter and return type.
Final Answer:
function repeatArray<T>(arr: Array<T>): Array<T> -> Option D
Quick Check:
Generic function array syntax = Array<T> [OK]
Hint: Use <T> and Array<T> for generic array functions [OK]
Common Mistakes:
Omitting generic type parameter <T>
Using non-generic Array without type
Incorrect parameter or return types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.