shelf.ts 533 B

1234567891011121314151617181920212223242526
  1. export interface ShelfItem {
  2. title: string;
  3. }
  4. /* There are lots of shelves in the library, and they can contain different
  5. types of objects.
  6. */
  7. export default class Shelf<T extends ShelfItem> {
  8. private _items: Array<T> = new Array<T>();
  9. add(item: T): void {
  10. this._items.push(item);
  11. }
  12. find(title: string): T {
  13. return this._items.filter(item => item.title === title)[0];
  14. }
  15. getFirst(): T {
  16. return this._items[0];
  17. }
  18. printTitles(): void {
  19. this._items.forEach(item => console.log(item.title));
  20. }
  21. }