TypeScript Tutorial

TypeScript Project: Library Catalog

A catalog of books with title, author, and year. Search by author and list every match.

What you will build

A catalog is an array of books. Each book has a title, an author, and a year. You print the full shelf, then search by author and list every match. Search uses includes so a last name can match inside a longer author field.

The shelf is hardcoded. Compile with Try it in TypeScript at/typescript/try. This is TypeScript, not a Python script and not a C++ file.

A Book interface

Three fields travel together. Title and author are strings. Year is a number. Printing one book is a single function so the catalog loop stays short.

Example

interface Book {
  title: string;
  author: string;
  year: number;
}

function printBook(b: Book): void {
  console.log(b.title + " - " + b.author + " (" + b.year + ")");
}

const b: Book = {
  title: "Programming TypeScript",
  author: "Boris Cherny",
  year: 2019,
};
printBook(b);

Fill a catalog

Book[] grows with push. A for...of loop prints every row. Pass the catalog into the print helper.

Example

interface Book {
  title: string;
  author: string;
  year: number;
}

function printBook(b: Book): void {
  console.log(b.title + " - " + b.author + " (" + b.year + ")");
}

function printCatalog(shelf: Book[]): void {
  console.log("Catalog (" + shelf.length + " books)");
  for (const b of shelf) {
    printBook(b);
  }
}

const shelf: Book[] = [];
shelf.push({ title: "Programming TypeScript", author: "Boris Cherny", year: 2019 });
shelf.push({ title: "Effective TypeScript", author: "Dan Vanderkam", year: 2019 });
shelf.push({ title: "TypeScript Cookbook", author: "Stefan Baumgartner", year: 2023 });
printCatalog(shelf);

Run the catalog at /typescript/try via Try it in TypeScript. Two titles will share an author in the complete program. That pair is what the search in the next sections must return.

Search with includes

author.includes(query) is true when the query is a substring. A query of Chernymatches Boris Cherny. An empty query matching every book is possible with includes; this project treats an empty query as no hits so a blank search does not dump the shelf.

Example

const author: string = "Boris Cherny";
const query: string = "Cherny";
if (author.includes(query)) {
  console.log("match");
} else {
  console.log("no match");
}

Complete catalog program

Walk the array. When the author field contains the query, print that book and count it. After the loop, print how many matches you found. Two searches run at the bottom of the file: one that hits twice, one that hits none.

Example

interface Book {
  title: string;
  author: string;
  year: number;
}

function printBook(b: Book): void {
  console.log(b.title + " - " + b.author + " (" + b.year + ")");
}

function searchByAuthor(shelf: Book[], query: string): void {
  console.log("Search author: " + query);
  if (query.length === 0) {
    console.log("Empty query, 0 matches");
    return;
  }
  let hits = 0;
  for (const b of shelf) {
    if (b.author.includes(query)) {
      printBook(b);
      hits = hits + 1;
    }
  }
  console.log(hits + " match(es)");
}

const shelf: Book[] = [];
shelf.push({ title: "Programming TypeScript", author: "Boris Cherny", year: 2019 });
shelf.push({ title: "Effective TypeScript", author: "Dan Vanderkam", year: 2019 });
shelf.push({ title: "Learning TypeScript", author: "Josh Goldberg", year: 2022 });
shelf.push({ title: "TypeScript in 50 Lessons", author: "Stefan Baumgartner", year: 2020 });
shelf.push({ title: "TypeScript Cookbook", author: "Stefan Baumgartner", year: 2023 });

console.log("All books");
for (const b of shelf) {
  printBook(b);
}
console.log("");
searchByAuthor(shelf, "Baumgartner");
console.log("");
searchByAuthor(shelf, "Ada");

The Baumgartner search prints two titles and 2 match(es). The Ada search prints0 match(es). Search is case-sensitive: baumgartner would miss these rows.

Common mistakes

  • Comparing authors with === only. Then Baumgartner fails againstStefan Baumgartner. Use includes when you want a substring.
  • Using indexOf and testing against 0. A match at the start of the string is index 0, which is a hit. The miss value is -1, or skip the index and use includes.
  • Forgetting to count hits. Printing books without a total makes an empty result look like a crashed loop.
  • Storing year as a string. Numeric year lets you add a filter such as “after 2020” later without parsing.

Practice

  1. Change the Ada search query to Goldberg and confirm one match prints.
  2. Add a sixth book by Dan Vanderkam and confirm that author search now lists two titles.
  3. Add a search-by-title function that also uses includes, and print only books with year >= 2022.

Next project: convert Celsius, Fahrenheit, and Kelvin with formatted output.

FAQ: TypeScript Project: Library Catalog

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript library project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript library project in this TypeScript TypeScript lesson (TypeScript Project: Library Catalog).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.