0
0
Typescriptprogramming~20 mins

Generic repository pattern in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Repository Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'}));
ATypeError at runtime
B"Saved item with id: undefined"
CCompilation error due to missing id property
D"Saved item with id: 1"
Attempts:
2 left
💡 Hint
Check how the generic type extends IEntity and the id property is accessed.
🧠 Conceptual
intermediate
1:30remaining
Purpose of generic repository pattern
What is the main purpose of using a generic repository pattern in TypeScript?
ATo replace all interfaces with classes
BTo enforce UI styling rules across components
CTo create a reusable data access layer for different entity types
DTo handle HTTP requests directly in components
Attempts:
2 left
💡 Hint
Think about code reuse and data management.
🔧 Debug
advanced
2: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'});
AReturns false because item with id 1 does not exist
BThrows runtime error because items array is undefined
CCompilation error due to missing constructor
DReturns true but does not update the item
Attempts:
2 left
💡 Hint
Check the initial state of the items array before update.
📝 Syntax
advanced
1:30remaining
Correct generic constraint syntax
Which option correctly declares a generic repository class constrained to entities with an id property?
Aclass GenericRepository<T where T.id: number> {}
Bclass GenericRepository<T extends {id: number}> {}
Cclass GenericRepository<T: {id: number}> {}
Dclass GenericRepository<T with id: number> {}
Attempts:
2 left
💡 Hint
Remember TypeScript syntax for generic constraints uses extends.
🚀 Application
expert
2: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);
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Track each save and delete operation carefully.