0
0
Typescriptprogramming~15 mins

Constructor parameter types in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Constructor Parameter Types in TypeScript
📖 Scenario: You are creating a simple program to manage a library's book records. Each book has a title and a number of pages.
🎯 Goal: Build a TypeScript class Book that uses constructor parameter types to store the title and page count of a book.
📋 What You'll Learn
Create a class called Book
Add a constructor with parameters title (string) and pages (number)
Store the parameters as public properties
Create an instance of Book with title 'The Hobbit' and pages 310
Print the book's title and pages
💡 Why This Matters
🌍 Real World
Classes with typed constructor parameters are common in real-world TypeScript programs to model data clearly and safely.
💼 Career
Understanding constructor parameter types helps you write clean, maintainable code in professional TypeScript projects.
Progress0 / 4 steps
1
Create the Book class with constructor parameters
Create a class called Book with a constructor that has two parameters: title of type string and pages of type number. Store these parameters as public properties directly in the constructor.
Typescript
Need a hint?

Use public before the constructor parameters to automatically create properties.

2
Create a Book instance
Create a variable called myBook and assign it a new Book object with the title 'The Hobbit' and pages 310.
Typescript
Need a hint?

Use new Book('The Hobbit', 310) to create the instance.

3
Access and print the book's properties
Use console.log to print the book's title and pages in the format: Title: The Hobbit, Pages: 310. Access the properties myBook.title and myBook.pages.
Typescript
Need a hint?

Use a template string with backticks to format the output.

4
Run the program to see the output
Run the program to display the book's title and pages exactly as: Title: The Hobbit, Pages: 310.
Typescript
Need a hint?

Check your console output matches exactly.