Consider the following TypeScript code using a generic interface for a collection. What will be logged to the console?
interface Collection<T> {
add(item: T): void;
get(index: number): T | undefined;
size(): number;
}
class SimpleCollection<T> implements Collection<T> {
private items: T[] = [];
add(item: T): void {
this.items.push(item);
}
get(index: number): T | undefined {
return this.items[index];
}
size(): number {
return this.items.length;
}
}
const numbers: Collection<number> = new SimpleCollection<number>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
console.log(numbers.get(1));
console.log(numbers.size());Remember that arrays are zero-indexed and get(1) returns the second item.
The SimpleCollection stores items in an array. Adding 10, 20, 30 means the array is [10, 20, 30]. get(1) returns the second item, which is 20. The size is 3 because three items were added.
Choose the correct statement about generic interfaces in TypeScript.
Think about how generics help reuse code for different types.
Generic interfaces let you write flexible contracts that work with any type you specify. They are not limited to primitives and methods can use the generic type parameter.
Examine the following TypeScript code. What error will the compiler report?
interface Collection<T> {
add(item: T): void;
get(index: number): T;
}
class MyCollection<T> implements Collection<T> {
private items: T[] = [];
add(item: T): void {
this.items.push(item);
}
get(index: number): T {
return this.items[index];
}
}
const col = new MyCollection<number>();
console.log(col.get(0));Think about what happens if the array is empty and you try to get an item.
The get method returns T but accessing an array index might return undefined if the index is out of bounds. This causes a type error because undefined is not assignable to T.
Choose the correct syntax for declaring a generic interface named Collection with a method add that accepts an item of generic type T.
Remember that the generic parameter should be declared on the interface, not the method, for this case.
Option B correctly declares a generic interface with a generic type parameter T. The method add uses that type. Options A and B incorrectly declare generics on the method or mismatch the interface generic. Option B uses T without declaring it.
Given the following code using a generic collection interface and class, how many items does the collection contain at the end?
interface Collection<T> {
add(item: T): void;
remove(item: T): boolean;
size(): number;
}
class AdvancedCollection<T> implements Collection<T> {
private items: T[] = [];
add(item: T): void {
if (!this.items.includes(item)) {
this.items.push(item);
}
}
remove(item: T): boolean {
const index = this.items.indexOf(item);
if (index >= 0) {
this.items.splice(index, 1);
return true;
}
return false;
}
size(): number {
return this.items.length;
}
}
const col = new AdvancedCollection<string>();
col.add("apple");
col.add("banana");
col.add("apple");
col.remove("banana");
col.add("cherry");
col.remove("date");
Consider how duplicates are handled and what happens when removing items not in the collection.
The collection starts empty. Adding "apple" adds it. Adding "banana" adds it. Adding "apple" again does nothing because duplicates are ignored. Removing "banana" removes it. Adding "cherry" adds it. Removing "date" does nothing because it is not present. Final items are ["apple", "cherry"] so size is 2.