A. The array items contain numbers, but the interface expects strings
B. The interface is missing the generic type parameter
C. The variable strings should be declared with type number
D. The interface property items should not be an array
Solution
Step 1: Check the generic type used
The variable strings is declared as Collection<string>, so items must be an array of strings.
Step 2: Check the assigned array type
The assigned array is [1, 2, 3], which is an array of numbers, not strings, causing a type mismatch error.
Final Answer:
The array items contain numbers, but the interface expects strings -> Option A
Quick Check:
Type mismatch in generic array = The array items contain numbers, but the interface expects strings [OK]
Hint: Check if array elements match generic type [OK]
Common Mistakes:
Ignoring type mismatch between generic and array
Forgetting to specify generic parameter
Assuming interface property can be non-array
5. You want to create a generic interface KeyValueCollection that stores key-value pairs where keys are strings and values can be any type T. Which interface definition correctly achieves this?
hard
A. interface KeyValueCollection<T> { [T: string]: string; }
B. interface KeyValueCollection<T> { keys: string[]; values: T[]; }
C. interface KeyValueCollection<T> { key: string; value: T; }
D. interface KeyValueCollection<T> { [key: string]: T; }
Solution
Step 1: Understand key-value pair requirement
The interface should allow any string as a key and associate it with a value of generic type T.
Step 2: Check index signature syntax
Using [key: string]: T; means any string key maps to a value of type T, which matches the requirement.
Step 3: Eliminate other options
interface KeyValueCollection<T> { keys: string[]; values: T[]; } separates keys and values arrays, which is less direct. interface KeyValueCollection<T> { key: string; value: T; } defines a single pair, not a collection. interface KeyValueCollection<T> { [T: string]: string; } has incorrect syntax.
Final Answer:
interface KeyValueCollection<T> { [key: string]: T; } -> Option D
Quick Check:
Index signature with generic value = interface KeyValueCollection<T> { [key: string]: T; } [OK]
Hint: Use index signature [key: string]: T for generic key-value [OK]
Common Mistakes:
Using arrays for keys and values separately
Defining only one key-value pair instead of collection
Incorrect index signature syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.