CodeBlanks Logo Icon CodeBlanks

Book model

Design book and member types.

Book model

We define three core types for a library. Book represents a book: id, title, author, and available (loan status). Member represents a member: id and name. Library holds an array of books.

interface Book { id: string; title: string; author: string; available: boolean; }
interface Member { id: string; name: string; }
interface Library { books: Book[]; }

Empty library

An empty library is a Library object whose books field is an empty array. The array grows as you add books.

const lib: Library = { books: [] };
console.log(lib.books.length);  // 0

Practice Write the library model

Define a Book interface: id: string, title: string, author: string, available: boolean. Define a Member interface: id: string, name: string. Define a Library interface: books: Book[]. Create an empty library and print the number of books.

Starter code

index.ts
// Book arayüzü tanımla (id, title, author, available)
// Member arayüzü tanımla (id, name)
// Library arayüzü tanımla (books alanı Book dizisi)
// boş kütüphane oluştur, kitap sayısını yazdır

Sample solution

index.ts
interface Book { id: string; title: string; author: string; available: boolean; }
interface Member { id: string; name: string; }
interface Library { books: Book[]; }

const lib: Library = { books: [] };
console.log(lib.books.length);

Expected output

0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which field tracks whether a book is borrowed?

  • available (boolean) Correct answer
  • title
  • author
  • id

Explanation: The available field is true when the book is on the shelf and false when it is borrowed. title, author, and id do not track loan status.

2 Complete the Book field

Question
interface Book { id: string; title: string; author: string; ____: boolean; }
Answer
interface Book { id: string; title: string; author: string; available: boolean; }

Explanation: The Book interface contains an available field; true means on shelf, false means borrowed. count, status, and borrowed are alternative names, but our design uses available.

3 Library model parts

Term Meaning
Book Book details (id, title, author, available)
Member Member details (id, name)
Library Book collection
available: boolean Loan status

All lessons in Beginner