Complete the code to declare a generic interface named Collection.
interface Collection<[1]> {
add(item: T): void;
remove(item: T): void;
size(): number;
}The generic type parameter T is commonly used to represent the type of items in the collection.
Complete the code to declare a class that implements the generic Collection interface.
class List<[1]> implements Collection<T> { private items: T[] = []; add(item: T): void { this.items.push(item); } remove(item: T): void { this.items = this.items.filter(i => i !== item); } size(): number { return this.items.length; } }
The class uses the same generic type parameter T as the interface it implements.
Fix the error in the method signature to correctly use the generic type parameter.
remove(item: [1]): void {
this.items = this.items.filter(i => i !== item);
}any or object.The method parameter must use the generic type T to match the collection item type.
Fill both blanks to create a generic function that returns the first item of a collection.
function getFirst<[1]>(collection: Collection<T>): [2] | undefined { // Assume collection has a method to get all items as array const items = (collection as any).items as T[]; return items[0]; }
The generic type parameter T is used both to declare the function and as the return type.
Fill all three blanks to define a generic interface with a method that returns an array of items.
interface Collection<[1]> { add(item: T): void; getAll(): [2][]; size(): [3]; }
string instead of number for size.The generic type T is used for the item type and array return type, while number is the return type for size().