A generic interface is a way to define an interface with a placeholder type that can be specified later. It allows the interface to work with any data type while keeping type safety.
Click to reveal answer
beginner
How do you declare a generic interface for a collection of items in TypeScript?
You declare it using angle brackets with a type parameter, for example: interface Collection<T> { add(item: T): void; get(index: number): T; } where T is the generic type.
Click to reveal answer
beginner
Why use a generic interface for collections instead of a specific type?
Using a generic interface lets you reuse the same code for different types of collections, like numbers, strings, or custom objects, without losing type safety or writing duplicate code.
Click to reveal answer
beginner
Example: What does this interface mean? interface Collection<T> { add(item: T): void; get(index: number): T; }
It means the Collection interface works with any type T. It has an add method to add an item of type T, and a get method to get an item of type T by index.
Click to reveal answer
beginner
How do you create a collection of strings using the generic interface Collection<T>?
You specify the type when using the interface, like <code>let stringCollection: Collection<string></code>. This means the collection will only hold strings.
Click to reveal answer
What does the T represent in interface Collection<T>?
AA function name
BA specific type like string
CA placeholder for any type
DA variable name
✗ Incorrect
T is a placeholder that stands for any type you want to use when you create a collection.
How do you specify the type when using a generic interface?
ABy writing the type inside angle brackets after the interface name
BBy writing the type before the interface name
CBy using a special keyword 'type'
DYou cannot specify the type
✗ Incorrect
You specify the type by putting it inside <> after the interface name, like Collection<number>.
Which of these is a benefit of using generic interfaces for collections?
AReuse code for different data types safely
BMake code run faster
CAvoid using any types
DAutomatically convert types
✗ Incorrect
Generic interfaces let you reuse the same code for many types while keeping type safety.
If you have let numbers: Collection<number>, what type of items can you add?
AOnly strings
BOnly numbers
CAny type
DOnly objects
✗ Incorrect
The collection is set to hold numbers only, so you can add only numbers.
What happens if you try to add a string to Collection<number>?
AThe program crashes
BIt works fine
CThe string is converted to number automatically
DTypeScript shows an error
✗ Incorrect
TypeScript will show a type error because the collection expects numbers only.
Explain what a generic interface for collections is and why it is useful.
Think about how you can write one interface that works for many types.
You got /4 concepts.
Describe how you would create and use a generic collection interface to store strings.
Focus on how to tell TypeScript the collection holds strings.
You got /4 concepts.
Practice
(1/5)
1. What does the <T> represent in a generic interface in TypeScript?
easy
A. A function inside the interface
B. A fixed type like number or string
C. A placeholder for any type that will be specified later
D. A variable name for a property
Solution
Step 1: Understand generic syntax
The <T> in TypeScript generics is a placeholder representing any type that will be provided when the interface is used.
Step 2: Differentiate from fixed types
Unlike fixed types like number or string, T is flexible and can represent any type, making the interface reusable.
Final Answer:
A placeholder for any type that will be specified later -> Option C
Quick Check:
Generic <T> = placeholder type [OK]
Hint: Remember <T> means 'any type' placeholder [OK]
Common Mistakes:
Thinking T is a fixed type
Confusing T with a function
Assuming T is a variable name
2. Which of the following is the correct syntax to declare a generic interface named Collection with a type parameter T?
easy
A. interface Collection<T> { items: T[]; }
B. interface Collection { items: T[]; }
C. interface Collection<T> { items: Array; }
D. interface Collection<T> { items: T; }
Solution
Step 1: Check generic declaration
The correct syntax to declare a generic interface uses <T> after the interface name, like interface Collection<T>.
Step 2: Verify property type
The property items should be an array of type T, so items: T[] is correct.
Final Answer:
interface Collection<T> { items: T[]; } -> Option A
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.