0
0
Typescriptprogramming~20 mins

Class property declarations in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Class property declarations
📖 Scenario: You are creating a simple program to represent a book in a library system. Each book has a title, an author, and a number of pages.
🎯 Goal: You will create a TypeScript class called Book with properties for title, author, and pages. Then you will create an instance of this class and display its details.
📋 What You'll Learn
Create a class called Book
Declare three properties inside the class: title (string), author (string), and pages (number)
Create a variable called myBook that is an instance of Book
Assign values to the properties of myBook
Print the book details in the format: Title: [title], Author: [author], Pages: [pages]
💡 Why This Matters
🌍 Real World
Classes with properties are used to model real-world objects in software, like books in a library or products in a store.
💼 Career
Understanding how to declare and use class properties is fundamental for object-oriented programming, which is widely used in software development jobs.
Progress0 / 4 steps
1
Create the Book class with properties
Create a class called Book with three properties: title of type string, author of type string, and pages of type number. Do not add any constructor or methods yet.
Typescript
Need a hint?

Use class Book {} and inside declare title: string;, author: string;, and pages: number;.

2
Create an instance of Book
Create a variable called myBook and assign it a new instance of the Book class.
Typescript
Need a hint?

Use const myBook = new Book(); to create the instance.

3
Assign values to myBook properties
Assign the following values to the properties of myBook: title = "The Great Gatsby", author = "F. Scott Fitzgerald", and pages = 180.
Typescript
Need a hint?

Use dot notation like myBook.title = "The Great Gatsby"; to assign values.

4
Print the book details
Write a console.log statement to print the book details in this exact format: Title: The Great Gatsby, Author: F. Scott Fitzgerald, Pages: 180 using the properties of myBook.
Typescript
Need a hint?

Use a template string with backticks and ${} to insert property values inside console.log.