📖 Scenario: Imagine you are building a simple app to manage books in a library. You want to keep track of each book's title and number of pages. Using typed classes helps you avoid mistakes and makes your code easier to understand.
🎯 Goal: You will create a typed class called Book with properties for title and pages. Then you will create an instance of this class and print its details.
📋 What You'll Learn
Create a class called Book with typed properties title: string and pages: number
Add a constructor to Book that sets title and pages
Create an instance of Book called myBook with title 'The TypeScript Guide' and pages 250
Print the book's title and pages using console.log
💡 Why This Matters
🌍 Real World
Typed classes are used in apps to keep data organized and safe, like managing books, users, or products.
💼 Career
Understanding typed classes is important for writing reliable TypeScript code in many software development jobs.
Progress0 / 4 steps
1
Create the typed class
Create a class called Book with typed properties title: string and pages: number. Add a constructor that takes title: string and pages: number and sets them to the class properties.
Typescript
Hint
Use class keyword, declare properties with types, and write a constructor method.
2
Create an instance of the class
Create an instance of Book called myBook with title 'The TypeScript Guide' and pages 250.
Typescript
Hint
Use new Book('The TypeScript Guide', 250) and assign it to myBook.
3
Access and print properties
Use console.log to print the book's title and pages from the myBook instance. Print the title first, then the pages.
Typescript
Hint
Use console.log(myBook.title) and console.log(myBook.pages).
4
Final output
Run the program to print the book's title and pages exactly as shown: first line The TypeScript Guide, second line 250.
Typescript
Hint
Make sure you run the program and see the two lines printed exactly.
Practice
(1/5)
1. Why are typed classes important in TypeScript?
easy
A. They make the code run faster at runtime.
B. They allow writing code without any rules.
C. They help catch errors before running the code.
D. They remove the need for functions.
Solution
Step 1: Understand typed classes role
Typed classes define what kind of data each property should hold, helping catch mistakes early.
Step 2: Identify the benefit
This early error detection happens before running the program, improving code safety.
Final Answer:
They help catch errors before running the code. -> Option C
Quick Check:
Typed classes catch errors early = B [OK]
Hint: Typed classes catch mistakes early, not speed up runtime [OK]
Common Mistakes:
Thinking typed classes speed up code execution
Believing typed classes remove the need for functions
Assuming typed classes allow ignoring data rules
2. Which of the following is the correct way to declare a typed class property in TypeScript?
easy
A. class User { name = string; }
B. class User { name: string; }
C. class User { string name; }
D. class User { name -> string; }
Solution
Step 1: Recall TypeScript property syntax
In TypeScript, properties are declared with a colon between name and type, like name: string;.
Step 2: Check each option
class User { name: string; } uses correct syntax. Options A, B, and C use invalid syntax.
Final Answer:
class User { name: string; } -> Option B
Quick Check:
Property declaration uses colon = D [OK]
Hint: Use colon between property and type in classes [OK]
Common Mistakes:
Using equals sign instead of colon for type
Placing type before property name
Using arrows or other symbols instead of colon
3. What will be the output of this TypeScript code?
The constructor sets name to 'Book' and price to 15.
Step 2: Check display method output
The display method returns a string combining name and price exactly as "Book costs $15".
Final Answer:
Book costs $15 -> Option A
Quick Check:
Display method returns formatted string = C [OK]
Hint: Check constructor values and string template output [OK]
Common Mistakes:
Expecting price to show decimals automatically
Confusing undefined property values
Thinking type mismatch causes runtime error here
4. Identify the error in this TypeScript class and fix it:
class Person {
age: number;
constructor(age: string) {
this.age = age;
}
}
medium
A. Change constructor parameter type to number
B. Change property type to string
C. Remove type annotations completely
D. No error, code is correct
Solution
Step 1: Compare property and constructor types
The property age is a number, but constructor expects a string parameter.
Step 2: Fix type mismatch
Change constructor parameter type to number to match property type.
Final Answer:
Change constructor parameter type to number -> Option A
Quick Check:
Property and parameter types must match = A [OK]
Hint: Match constructor parameter types with property types [OK]
Common Mistakes:
Ignoring type mismatch between property and parameter
Changing property type unnecessarily
Assuming no error when types differ
5. You want to create a typed class in TypeScript that holds a list of users with their names and ages. Which approach best ensures type safety and easy maintenance?
hard
A. Use a class with properties typed as strings only.
B. Use a class with any type for properties to allow flexibility.
C. Use plain JavaScript objects without types for faster coding.
D. Use a class with typed properties and an array of user objects typed with an interface.
Solution
Step 1: Consider type safety and maintenance
Typed properties and interfaces help catch errors and make code clear for teamwork.
Step 2: Evaluate options
A class with typed properties and an array of user objects typed with an interface ensures safety and clarity. Other options sacrifice safety or clarity.
Final Answer:
Use a class with typed properties and an array of user objects typed with an interface. -> Option D
Quick Check:
Typed classes + interfaces = best practice [OK]
Hint: Combine typed classes with interfaces for clear, safe code [OK]
Common Mistakes:
Using any type loses type safety
Ignoring interfaces reduces code clarity
Typing all properties as strings limits data accuracy
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.