0
0
Typescriptprogramming~30 mins

Migrating JavaScript to TypeScript - Mini Project: Build & Apply

Choose your learning style9 modes available
Migrating JavaScript to TypeScript
📖 Scenario: You have a simple JavaScript program that manages a list of books. You want to improve the code by migrating it to TypeScript to catch errors early and add type safety.
🎯 Goal: Convert a JavaScript program managing books into TypeScript by adding type annotations and interfaces step-by-step.
📋 What You'll Learn
Create an array of book objects with title and author properties
Add a TypeScript interface for the book objects
Use the interface to type the array of books
Print the list of books with their titles and authors
💡 Why This Matters
🌍 Real World
Migrating JavaScript codebases to TypeScript is common in real projects to improve maintainability and reduce bugs.
💼 Career
Many companies use TypeScript for frontend and backend development, so knowing how to migrate and add types is a valuable skill.
Progress0 / 4 steps
1
Create the initial JavaScript data
Create a variable called books and assign it an array with these exact objects: { title: "The Hobbit", author: "J.R.R. Tolkien" } and { title: "1984", author: "George Orwell" }.
Typescript
Need a hint?

Use const books = [ ... ] with two objects inside the array.

2
Add a TypeScript interface for books
Create an interface called Book with two string properties: title and author.
Typescript
Need a hint?

Use interface Book { title: string; author: string; }.

3
Type the books array using the interface
Change the declaration of books to be an array of Book by adding the type annotation : Book[].
Typescript
Need a hint?

Write const books: Book[] = [...] to specify the array type.

4
Print the list of books
Use a for loop with variables book to iterate over books and print each book's title and author using console.log with the format: Title: [title], Author: [author].
Typescript
Need a hint?

Use for (const book of books) and console.log(`Title: ${book.title}, Author: ${book.author}`).