Bird
Raised Fist0
Typescriptprogramming~20 mins

Array type annotation syntax in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Type Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code with array type annotation?
Consider the following TypeScript code snippet. What will be logged to the console?
Typescript
function sumNumbers(numbers: number[]): number {
  return numbers.reduce((acc, val) => acc + val, 0);
}

console.log(sumNumbers([1, 2, 3, 4]));
A10
BTypeError at runtime
Cundefined
DCompilation error due to wrong type annotation
Attempts:
2 left
💡 Hint
Look at how the array is typed and how reduce sums the values.
❓ Predict Output
intermediate
2:00remaining
What error does this TypeScript code raise?
What error will this TypeScript code produce when compiled?
Typescript
let names: string[] = ["Alice", "Bob", 42];
ARuntime error: Cannot mix types in array
BSyntaxError: Unexpected number in array
CNo error, compiles fine
DType 'number' is not assignable to type 'string'.
Attempts:
2 left
💡 Hint
Check the array type and the types of elements inside.
🔧 Debug
advanced
2:00remaining
Why does this array type annotation cause a compilation error?
Identify the reason for the compilation error in this TypeScript code snippet.
Typescript
const values: Array<number> = [1, 2, "3", 4];
AArray<number> syntax is invalid in TypeScript.
BThe string "3" is not assignable to type number in the array.
CMissing semicolon after array declaration causes error.
DThe array must be declared with square brackets, not Array<>.
Attempts:
2 left
💡 Hint
Check the types of elements inside the array compared to the annotation.
📝 Syntax
advanced
2:00remaining
Which option correctly annotates an array of booleans in TypeScript?
Choose the correct TypeScript syntax to declare a variable named flags as an array of booleans.
Alet flags: boolean[] = [true, false, true];
Blet flags: [boolean] = [true, false, true];
Clet flags: Array = [true, false, true];
Dlet flags: boolean = [true, false, true];
Attempts:
2 left
💡 Hint
Remember the two common ways to annotate arrays in TypeScript.
🚀 Application
expert
3:00remaining
How many items are in the resulting array after this TypeScript code runs?
Given the following TypeScript code, how many items does the variable result contain?
Typescript
const mixedArray: (string | number)[] = ["a", 1, "b", 2];
const result: string[] = mixedArray.filter((item): item is string => typeof item === "string");
A0
B4
C2
DType error at compile time
Attempts:
2 left
💡 Hint
Look at the filter condition and the types in the original array.

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

  1. Step 1: Understand array type syntax

    In TypeScript, to declare an array of a specific type, you can use type[] or Array<type>.
  2. 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<>.
  3. Final Answer:

    let nums: number[]; -> Option D
  4. 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

  1. Step 1: Recall generic array syntax

    The generic syntax for arrays in TypeScript is Array<type>, with angle brackets <>.
  2. 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.
  3. Final Answer:

    let names: Array<string>; -> Option C
  4. 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

  1. Step 1: Understand array indexing

    Arrays in TypeScript (and JavaScript) start at index 0. So, fruits[0] is 'apple', fruits[1] is 'banana'.
  2. Step 2: Identify the logged value

    The code logs fruits[1], which is 'banana'.
  3. Final Answer:

    banana -> Option B
  4. 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

  1. Step 1: Check array type and elements

    The array is declared as Array<number>, so all elements must be numbers.
  2. Step 2: Identify the wrong element

    The element '30' is a string because of quotes, which conflicts with the number type.
  3. Step 3: Fix the error

    Remove quotes from '30' to make it a number: 30.
  4. Final Answer:

    Remove quotes from '30' to make it a number -> Option A
  5. 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

  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    let matrix: Array; -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard