0
0
Typescriptprogramming~15 mins

Creating type aliases in Typescript - Try It Yourself

Choose your learning style9 modes available
Creating type aliases
📖 Scenario: You are building a simple program to manage a list of books in a library. Each book has a title and an author.
🎯 Goal: Create a type alias for a book object, then use it to define a list of books and print their titles.
📋 What You'll Learn
Create a type alias called Book with properties title and author, both strings.
Create a variable called library which is an array of Book objects.
Add at least two books to the library array with exact titles and authors.
Use a for loop to print each book's title.
💡 Why This Matters
🌍 Real World
Type aliases help you clearly define the shape of data objects, making your code easier to understand and less error-prone.
💼 Career
Many programming jobs require working with typed data structures to build reliable applications, especially in TypeScript.
Progress0 / 4 steps
1
Create the Book type alias
Create a type alias called Book with two string properties: title and author.
Typescript
Need a hint?

Use the type keyword to create a type alias with properties.

2
Create the library array
Create a variable called library that is an array of Book objects. Add these two books exactly: { title: "The Hobbit", author: "J.R.R. Tolkien" } and { title: "1984", author: "George Orwell" }.
Typescript
Need a hint?

Use const library: Book[] = [...] to create the array with the two book objects.

3
Loop through library to print titles
Use a for loop with variable book to iterate over library and print each book's title using console.log.
Typescript
Need a hint?

Use for (const book of library) to loop and console.log(book.title) to print.

4
Print the book titles
Run the program to print the titles of the books in library. The output should be exactly:
The Hobbit
1984
Typescript
Need a hint?

Run the program and check the console output matches the two book titles on separate lines.