📖 Scenario: You are creating a simple system to store information about vehicles. You want to start with a basic vehicle interface and then add more details for specific types of vehicles.
🎯 Goal: Build TypeScript interfaces where one interface extends another to add more properties. Then create an object using the extended interface and display its details.
📋 What You'll Learn
Create a base interface called Vehicle with properties make and model as strings
Create an interface called Car that extends Vehicle and adds a doors property as a number
Create a variable called myCar of type Car with specific values
Print the details of myCar using a template string
💡 Why This Matters
🌍 Real World
Extending interfaces helps organize related data in software, like vehicles, users, or products, making code easier to maintain and expand.
💼 Career
Understanding interface extension is important for TypeScript developers working on scalable applications with clear data structures.
Progress0 / 4 steps
1
Create the base interface
Create an interface called Vehicle with two string properties: make and model.
Typescript
Hint
Use the interface keyword followed by the interface name and curly braces to define properties.
2
Extend the base interface
Create an interface called Car that extends Vehicle and adds a doors property of type number.
Typescript
Hint
Use interface Car extends Vehicle and add the new property inside curly braces.
3
Create an object using the extended interface
Create a variable called myCar of type Car and assign it an object with make as "Toyota", model as "Corolla", and doors as 4.
Typescript
Hint
Use const myCar: Car = { ... } and include all required properties.
4
Display the car details
Write a console.log statement to print: "My car is a [make] [model] with [doors] doors." using the myCar object and a template string.
Typescript
Hint
Use console.log with backticks and ${} to insert variables.
Practice
(1/5)
1. What does the extends keyword do when used with interfaces in TypeScript?
easy
A. It merges two unrelated interfaces without inheritance.
B. It deletes properties from an existing interface.
C. It creates a new interface that inherits properties from an existing interface.
D. It converts an interface into a class.
Solution
Step 1: Understand the role of extends in interfaces
The extends keyword allows one interface to inherit properties from another, creating a new interface that includes all properties of the original.
Step 2: Compare options with this understanding
Only It creates a new interface that inherits properties from an existing interface. correctly describes inheritance of properties. Other options describe unrelated or incorrect behaviors.
Final Answer:
It creates a new interface that inherits properties from an existing interface. -> Option C
Quick Check:
Extending interfaces means inheritance = D [OK]
Hint: Remember: extends means inherit properties [OK]
Common Mistakes:
Thinking extends deletes properties
Confusing interfaces with classes
Believing extends merges unrelated interfaces
2. Which of the following is the correct syntax to extend an interface Person with a new interface Employee?
easy
A. interface Employee extends Person {}
B. interface Employee inherits Person {}
C. interface Employee : Person {}
D. interface Employee < Person {}
Solution
Step 1: Recall TypeScript syntax for extending interfaces
The correct keyword to extend an interface is extends, used as interface NewInterface extends ExistingInterface {}.
Step 2: Check each option
interface Employee extends Person {} uses the correct syntax. Options B, C, and D use invalid keywords or symbols.
Final Answer:
interface Employee extends Person {} -> Option A
Quick Check:
Use 'extends' keyword for interface inheritance [OK]
Hint: Use 'extends' keyword exactly for interfaces [OK]
Common Mistakes:
Using 'inherits' instead of 'extends'
Using ':' like in classes
Using symbols like '<' incorrectly
3. Given the code below, what is the type of employee?