0
0
Typescriptprogramming~20 mins

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

Choose your learning style9 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.