0
0
Typescriptprogramming~15 mins

Parameter properties shorthand in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Parameter Properties Shorthand in TypeScript
📖 Scenario: Imagine you are creating a simple app to manage books in a library. Each book has a title and an author.
🎯 Goal: You will create a Book class using TypeScript's parameter properties shorthand to store the title and author. Then, you will create an instance and display its details.
📋 What You'll Learn
Create a Book class with a constructor using parameter properties shorthand
The constructor should have two parameters: title and author, both strings
Create an instance of Book with title 'The Hobbit' and author 'J.R.R. Tolkien'
Print the book's title and author using the instance properties
💡 Why This Matters
🌍 Real World
Parameter properties shorthand is commonly used in TypeScript to quickly create classes that hold data, such as models in apps, configuration objects, or simple data containers.
💼 Career
Understanding this shorthand helps you write cleaner and more maintainable TypeScript code, a valuable skill for frontend and backend developers working with modern frameworks and libraries.
Progress0 / 4 steps
1
Create the Book class with a constructor
Create a class called Book with a constructor that takes two parameters: title and author, both of type string. Do not add any code inside the constructor yet.
Typescript
Need a hint?

Define a class with a constructor that accepts two string parameters named title and author.

2
Use parameter properties shorthand in the constructor
Modify the Book class constructor to use parameter properties shorthand by adding public before both parameters title and author.
Typescript
Need a hint?

Add the public keyword before each parameter to automatically create and assign class properties.

3
Create an instance of Book
Create a variable called myBook and assign it a new Book instance with the title 'The Hobbit' and author 'J.R.R. Tolkien'.
Typescript
Need a hint?

Use the new keyword to create an instance and assign it to myBook.

4
Print the book's title and author
Write a console.log statement to print the book's title and author from the myBook instance in the format: Title: The Hobbit, Author: J.R.R. Tolkien.
Typescript
Need a hint?

Use a template string to include myBook.title and myBook.author in the output.