typescript task
Private research annotation
Sign in to add a private research label or note.
- User
typescript task
Assistant
Here's a TypeScript task for you, complete with requirements, a scenario, and deliverables. Please follow the instructions carefully.
Task Title: Implementing a Simple TypeScript Library for Managing Books
Scenario: You are tasked with creating a basic TypeScript library that manages books in a virtual library. The library should allow for adding, removing, and listing books. Each book has a title, author, publication year, and a unique identifier (ID).
Requirements:
- Book Interface/Class:
- Define an interface or class
Bookwith properties:id(unique identifier, string)title(string)author(string)publicationYear(number)
- Define an interface or class
Example:
typescriptinterface Book { id: string; title: string; author: string; publicationYear: number; }- Library Class:
- Create a class
Librarywith methods:addBook(book: Book): Adds a book to the library. If a book with the sameidalready exists, it should not be added and a meaningful error should be thrown.removeBook(id: string): Removes a book by itsid. If no book with the givenidexists, a meaningful error should be thrown.listBooks(): Returns an array of all books in the library.
- Create a class
Example:
typescriptclass Library { private books: Book[] = []; addBook(book: Book) { if (this.books.find((b) => b.id === book.id)) { throw new BookAlreadyExistsError(`Book with ID ${book.id} already exists.`); } this.books.push(book); } removeBook(id: string) { const bookIndex = this.books.findIndex((book) => book.id === id); if (bookIndex === -1) { throw new BookNotFoundError(`Book with ID ${id} not found.`); } this.books.splice(bookIndex, 1); } listBooks() { return this.books; } }- Book Interface/Class:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.