0
0
Typescriptprogramming~15 mins

Interface declaration syntax in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Interface declaration syntax
📖 Scenario: You are creating a simple program to store information about a book in a library system.
🎯 Goal: Build a TypeScript interface to define the structure of a book object and use it to create a book variable.
📋 What You'll Learn
Create an interface named Book with properties title (string), author (string), and year (number).
Create a variable named myBook of type Book with the exact values: title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', year: 1925.
Print the myBook variable to the console.
💡 Why This Matters
🌍 Real World
Interfaces help programmers define clear rules for data structures, making code easier to understand and less error-prone.
💼 Career
Knowing how to declare and use interfaces is essential for TypeScript developers working on large projects or collaborating in teams.
Progress0 / 4 steps
1
Create the Book interface
Write an interface named Book with three properties: title of type string, author of type string, and year of type number.
Typescript
Need a hint?

Use the interface keyword followed by the interface name and curly braces to define properties with their types.

2
Create a myBook variable of type Book
Create a variable named myBook of type Book and assign it an object with title set to 'The Great Gatsby', author set to 'F. Scott Fitzgerald', and year set to 1925.
Typescript
Need a hint?

Use const myBook: Book = { ... } with the exact property names and values.

3
Add a function to display book info
Write a function named displayBook that takes a parameter book of type Book and returns a string formatted as "Title: [title], Author: [author], Year: [year]" using template literals.
Typescript
Need a hint?

Use a function with a typed parameter and return a formatted string using backticks and ${} placeholders.

4
Print the book information
Use console.log to print the result of calling displayBook with myBook as the argument.
Typescript
Need a hint?

Call console.log(displayBook(myBook)) to show the formatted book info.