Challenge - 5 Problems
Generic Repository Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of generic repository save method
What is the output of this TypeScript code using a generic repository pattern?
Typescript
interface IEntity {
id: number;
}
class GenericRepository<T extends IEntity> {
private items: T[] = [];
save(item: T): string {
this.items.push(item);
return `Saved item with id: ${item.id}`;
}
}
const repo = new GenericRepository<{id: number; name: string}>();
console.log(repo.save({id: 1, name: 'Test'}));Attempts:
2 left
💡 Hint
Check how the generic type extends IEntity and the id property is accessed.
✗ Incorrect
The generic type T extends IEntity, which requires an id property. The save method pushes the item and returns a string with the id. Since the object passed has id: 1, the output is 'Saved item with id: 1'.
🧠 Conceptual
intermediate1:30remaining
Purpose of generic repository pattern
What is the main purpose of using a generic repository pattern in TypeScript?
Attempts:
2 left
💡 Hint
Think about code reuse and data management.
✗ Incorrect
The generic repository pattern allows writing one class to handle data operations for many entity types, improving code reuse and consistency.
🔧 Debug
advanced2:00remaining
Identify the error in generic repository update method
What error will this TypeScript code produce when trying to update an item in the generic repository?
Typescript
interface IEntity {
id: number;
}
class GenericRepository<T extends IEntity> {
private items: T[] = [];
update(item: T): boolean {
const index = this.items.findIndex(i => i.id === item.id);
if (index !== -1) {
this.items[index] = item;
return true;
}
return false;
}
}
const repo = new GenericRepository<{id: number; name: string}>();
repo.update({id: 1, name: 'Updated'});Attempts:
2 left
💡 Hint
Check the initial state of the items array before update.
✗ Incorrect
The items array is empty initially, so findIndex returns -1. The update method returns false because no item with id 1 exists to update.
📝 Syntax
advanced1:30remaining
Correct generic constraint syntax
Which option correctly declares a generic repository class constrained to entities with an id property?
Attempts:
2 left
💡 Hint
Remember TypeScript syntax for generic constraints uses extends.
✗ Incorrect
Option B uses the correct TypeScript syntax for generic constraints. Other options use invalid syntax.
🚀 Application
expert2:30remaining
Number of items after operations in generic repository
Given this TypeScript code using a generic repository, how many items are in the repository after all operations?
Typescript
interface IEntity { id: number; }
class GenericRepository<T extends IEntity> {
private items: T[] = [];
save(item: T): void {
this.items.push(item);
}
delete(id: number): boolean {
const index = this.items.findIndex(i => i.id === id);
if (index !== -1) {
this.items.splice(index, 1);
return true;
}
return false;
}
count(): number {
return this.items.length;
}
}
const repo = new GenericRepository<{id: number; name: string}>();
repo.save({id: 1, name: 'A'});
repo.save({id: 2, name: 'B'});
repo.delete(1);
repo.save({id: 3, name: 'C'});
repo.delete(4);
const total = repo.count();
console.log(total);Attempts:
2 left
💡 Hint
Track each save and delete operation carefully.
✗ Incorrect
After saving items with id 1 and 2, deleting id 1 removes one item, then saving id 3 adds another. Deleting id 4 does nothing. So total items are 2.